Scott on Writing

Musings on technical writing...

Outputting XML in a Visually Pleasing Manner

Another XML challenge: given an XML string, like:

<book><title>1984</title><author>Orwell</author></book>

What's the easiest way to display the XML in a friendly, indented way, like:

<book>
  <title>
    1984
  </title>
  <author>
    Orwell
  </author>
</book>

Here is my approach, let me know if you know of one better. Essentially, what I do is the following:

  1. Populate an XmlDocument with the XML data.
  2. Create an XmlTextWriter instance that writes to a StringWriter and set its Formatting property to Formatting.Indented
  3. Save the XmlDocument's data to the XmlTextWriter via the Save() method
  4. Get the prettily formatted XML by calling the StringWriter's ToString() method.

Is there a more efficient way? It seems rather unfortunate to have to load the entire XML in an XmlDocument instance first. I guess one could use an XmlTextReader to read through each node, and then write it to the XmlTextWriter, but I don't see a method that does this for me automatically. Anywho, here's the code I use:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);

StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
writer.Formatting = Formatting.Indented;

xmlDoc.Save(writer);
Console.WriteLine(sw.ToString());

posted on Thursday, October 16, 2003 2:11 PM

Feedback

# re: Outputting XML in a Visually Pleasing Manner 8/11/2004 2:13 PM Jake

THANKS!!!! Good code ! I wanted this to "pretty up" xml displayed in a textarea.

# re: Outputting XML in a Visually Pleasing Manner 8/12/2004 10:07 AM Jake

Hey dude, just found out this way to also output "pretty" xml:

dsAuthors is a dataset containing the XML

System.IO.StringWriter swXML = new System.IO.StringWriter();
dsAuthors.WriteXmlSchema(swXML);
textBox1.Text = swXML.ToString();

For the full code:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkWalkthroughAccessingXMLData.asp

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<July 2008>
SMTWTFS
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Comment Stats

DayTotal% of Total
Sunday 1896.8%
Monday 39014.0%
Tuesday 46916.8%
Wednesday 51518.5%
Thursday 54419.5%
Friday 50818.2%
Saturday 1706.1%
Total 2785100.0%

Hour1Total% of Total
12:00 AM 682.4%
1:00 AM 712.5%
2:00 AM 632.3%
3:00 AM 752.7%
4:00 AM 572.0%
5:00 AM 1093.9%
6:00 AM 1114.0%
7:00 AM 1615.8%
8:00 AM 1756.3%
9:00 AM 1505.4%
10:00 AM 1736.2%
11:00 AM 1826.5%
12:00 PM 1906.8%
1:00 PM 1766.3%
2:00 PM 1605.7%
3:00 PM 1324.7%
4:00 PM 1124.0%
5:00 PM 983.5%
6:00 PM 913.3%
7:00 PM 993.6%
8:00 PM 853.1%
9:00 PM 802.9%
10:00 PM 833.0%
11:00 PM 843.0%
Total 2785100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.14144
Monday 5.35353
Tuesday 4.35444
Wednesday 7.58644
Thursday 6.87625
Friday 5.45414
Saturday 5.03161
Total 5.802785

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.45109
9:00 AM 6.34279
10:00 AM 6.41250
11:00 AM 4.28184
12:00 PM 6.98342
1:00 PM 2.87112
2:00 PM 5.29222
3:00 PM 8.54299
4:00 PM 3.9190
5:00 PM 5.78156
6:00 PM 4.52113
7:00 PM 9.32177
8:00 PM 9.06154
9:00 PM 5.14113
10:00 PM 6.2381
11:00 PM 4.5732
Total 5.802785

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles