473,499 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatting XML Output From XmlTextWriter

I am writing and .xml file and am not getting the formatting I would
like. The portion of the code that is giving me problems is as
follows;

XmlTextWriter tw = new XmlTextWriter(filename);

tw.Formatting = Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement("MyRoot");
tw.WriteStartElement("MyString);
tw.WriteString("This is the string");
tw.WriteEndElement();
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();

The output I get is formatted as follows.

<?xml version="1.0"?>
<MyRoot>
<MyString>This is the string</MyString>
</MyRoot>

My questions is how do I get the MyString tags and the string itself
to each appear on consecutive lines, i.e.

<MyString>
This is the string
</MyString>

Ken Wilson
Seeking viable employment in Victoria, BC
Nov 17 '05 #1
2 6306


Ken Wilson wrote:
I am writing and .xml file and am not getting the formatting I would
like. XmlTextWriter tw = new XmlTextWriter(filename);

tw.Formatting = Formatting.Indented;
tw.WriteStartElement("MyString);
tw.WriteString("This is the string");
tw.WriteEndElement(); The output I get is formatted as follows. <MyString>This is the string</MyString> My questions is how do I get the MyString tags and the string itself
to each appear on consecutive lines, i.e.

<MyString>
This is the string
</MyString>


There is nothing in the XmlTextWriter implementation that gives you that
formatting, it does not make much sense to have white space added
automatically to text content in an element.
If you want that you will need your own implementation of XmlWriter or
at least extend XmlTextWriter.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 17 '05 #2
On Thu, 03 Nov 2005 20:33:08 +0100, Martin Honnen <ma*******@yahoo.de>
wrote:


Ken Wilson wrote:
I am writing and .xml file and am not getting the formatting I would
like.

XmlTextWriter tw = new XmlTextWriter(filename);

tw.Formatting = Formatting.Indented;


tw.WriteStartElement("MyString);
tw.WriteString("This is the string");
tw.WriteEndElement();

The output I get is formatted as follows.

<MyString>This is the string</MyString>

My questions is how do I get the MyString tags and the string itself
to each appear on consecutive lines, i.e.

<MyString>
This is the string
</MyString>


There is nothing in the XmlTextWriter implementation that gives you that
formatting, it does not make much sense to have white space added
automatically to text content in an element.
If you want that you will need your own implementation of XmlWriter or
at least extend XmlTextWriter.


Thank you for your reply. You confirm what I suspected. At this time
I will take the stance that if someone wants it 'pretty' printed they
can format it however they wish when they parse it. Once again, your
reply is appreciated.

Ken Wilson
Seeking viable employment in Victoria, BC
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2140
by: cnu | last post by:
Hi, I have to make a formatted (indented) xml file like this : <?xml version="1.0"?> <Message> <Parent index="0" length="6">123 <Child1 index="0,1" length="6">abcd</Child1> <Child2...
6
4824
by: Ot | last post by:
I have an xml document similar to this <all> <one> <options attr1="1" attr2="2" /> <item name="a name" attr1="1" /> <item name="another" attr1="2"/> </one> <two> <options attr1="1"...
2
2512
by: Miki Szinegh | last post by:
Hello All, I have an asp.net search application that brings up a datagrid of results, with one of the fields being a hyperlink to an XML document, of which there are thousands. By clicking this...
2
2032
by: vector | last post by:
Here is a function more or less exactly as I found it from somewhere on the internet. static string BeautifyXML(string sXML) { string result = ""; System.IO.MemoryStream ms = new...
1
10144
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load...
4
1243
by: Adrian Parker | last post by:
I have an xml document object with xml in it, how can I convert the xml to a string and format it so that it is is indented correctly. I've looked at XMLTextWriter and the Format property, but am...
3
1144
by: Urs Vogel | last post by:
Hi When using the XmlDocument.OuterXml property after adding some nodes and attributes, it returns the entire document correctly, but it's actually all on one line. How can I format the document...
0
1215
by: marfi95 | last post by:
I have an xml file that I want to maintain the whitespace on and spacing between my sections. I have something like this. <!-- Comment lines A --> <!-- Comment lines A --> <A> <B>test</B>...
1
3082
by: JAM | last post by:
I'm trying to code some directory structure as my output / input file using XML. I would like to see formatiing with indentations. mimicking directory tree structure. Unfortunately the code such as...
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.