Peter Petrov’s Weblog

var me = from practice in programming where practice.IsBestPractice && practice.UseLambda select practice.OptimalPerformance;

Useful method – 5 of N – Format/Beautify XML June 24, 2008

Filed under: C#,XML — ppetrov @ 7:27 pm
Tags: , , ,

Here’s a method to format a XML string or a XML file.

    public static string Format(string xmlContents)
    {
        StringBuilder buffer = new StringBuilder();

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlContents);

        using (var writer = XmlTextWriter.Create(buffer, new XmlWriterSettings() { Indent = true }))
        {
            doc.Save(writer);
        }

        return buffer.ToString();
    }

    public static void FormatFile(string inputFile)
    {
        FormatFile(inputFile, inputFile);
    }

    public static void FormatFile(string inputFile, string outputFile)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(inputFile);

        using (var writer = XmlTextWriter.Create(outputFile, new XmlWriterSettings() { Indent = true }))
        {
            doc.Save(writer);
        }
    }

I’ve tried to optimize these methods by replacing the XmlDocument with XPathDocument. The documentation of XPathDocument says:

Provides a fast, read-only, in-memory representation of an XML document using the XPath data model.

My first thought was that the read-only nature of XPathDocument will speed up my code, so I end up with this method.

    public static string FormatUsingXPath(string xmlContents)
    {
        StringBuilder buffer = new StringBuilder();

        using (var writer = XmlTextWriter.Create(buffer, new XmlWriterSettings() { Indent = true }))
        {
            using (XmlTextReader reader = new XmlTextReader(xmlContents, XmlNodeType.Document, null))
            {
                XPathDocument doc = new XPathDocument(reader);
                writer.WriteNode(doc.CreateNavigator(), false);
            }
        }

        return buffer.ToString();
    }

Unfortunately the XPath version is 10% slower. I think the difference comes from the creation of the XmlTextReader.

If we apply the method on the following unformatted XML


<?xml version="1.0"?>
<catalog>
 <book id="bk101">
 <author>Gambardella, Matthew</author><title>XML Developer's Guide</title>

 <genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
 <description>An in-depth look at creating applications
 with XML.</description>

 </book>
</catalog>

we will receive a well formatted version of our input XML.

Unfortunately there’s a problem with wordpress.com XML formatting and the well formatted XML isn’t shown as expected.

Advertisements
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s