469,923 Members | 1,459 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

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 6099


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by cnu | last post: by
6 posts views Thread by Ot | last post: by
2 posts views Thread by Miki Szinegh | last post: by
2 posts views Thread by vector | last post: by
1 post views Thread by Riko Eksteen | last post: by
4 posts views Thread by Adrian Parker | last post: by
3 posts views Thread by Urs Vogel | last post: by
reply views Thread by marfi95 | last post: by
1 post views Thread by JAM | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.