Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Indenting xml document

Status
Not open for further replies.

juhaka

Programmer
Nov 14, 2002
26
FI
Hi!

Is there any way to indent xml in XmlDocument object?
I have a xml document in XmlDocument object, and there is no whitespaces. When I print it (to console), it is hard to read. I think, indenting the document makes it much more readable.
XmlTextWriter class has a property called Formatting, which can get value Formatting.Indented. Is there any similar way for XmlDocument?

br. Juha Ka
 
I'm afraid not. The XmlDocument class is a logical representation of an XML document, and as such, formatting is not a concern of it, as it's primarily used by application code. If you want it to be "pretty printed" you would need to either write code yourself to do it, or use something like the XmlTextWriter.

I don't know if your question is simply of of desiring a more attractive presentation of your XML info, or if you're sending a file to someone who is reading it as if it were a flat file (like a .CSV file or .TXT file). If the latter, please point them to a XML parser. I've had to deal with that issue myself, and it's a real pain to deal with someone who won't use a parser to read the files.

You should also be aware that as far as XML is concerned, it doesn't need any carriage-returns, linefeeds, or tabs/whitespace between elements. You could produce XML that displays all on one line, and it would still be valid.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks, Chip

There is no need to read XML data 'manually', but only for debugging purpose. Maybe, I will write the indenting code myself later, maybe...
But now, I can (have to) live with this XmlDocument -object ;O)!

Thanks again!

br. Juha Ka
 
Hip hey hurrah!

I found a one way to indent the data of XmlDocument -object! I don't is there any risks to do so, but it seems to work.

XmlDocument doc = new XmlDocument();
doc.LoadXml(&quot;<?xml version=\&quot;1.0\&quot; encoding=\&quot;utf-8\&quot; standalone=\&quot;yes\&quot;?>&quot; +
&quot;<author>&quot; +
&quot;<name title=\&quot;foo\&quot;>&quot; +
&quot;<first-name>Eva</first-name>&quot;+
&quot;<last-name>Corets</last-name>&quot; +
&quot;</name>&quot; +
&quot;</author>&quot;);

Console.WriteLine(&quot;InnerXml before...&quot;);
Console.WriteLine(doc.InnerXml);

MemoryStream mst = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mst, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
doc.WriteContentTo(writer);
writer.Flush();

//reading the stream
StreamReader sr = new StreamReader(mst);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
string xmlStr = sr.ReadToEnd();

mst.Close();
sr.Close();
writer.Close();

doc.PreserveWhitespace = true;
doc.InnerXml = xmlStr;
Console.WriteLine(&quot;\nInnerXml after...&quot;);
Console.WriteLine(doc.InnerXml);


This gives ...

InnerXml before...
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; standalone=&quot;yes&quot;?><author><name title=&quot;foo&quot;><first-name>Eva</first-name><last-name>Corets</last-name></name></author>

InnerXml after...
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; standalone=&quot;yes&quot;?>
<author>
<name title=&quot;foo&quot;>
<first-name>Eva</first-name>
<last-name>Corets</last-name>
</name>
</author>


Here in the code is not error handling, but it has to be added, if yuo use this!
If you have comments, please notify me by replying this message.

br. Juha Ka

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top