Formatting XML Output From XmlTextWriter

K

Ken Wilson

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
 
M

Martin Honnen

Ken said:
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.
 
K

Ken Wilson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top