tags:

views:

787

answers:

5

I have an XML string as such:

<?xml version='1.0'?><response><error code='1'> Success</error></response>

There are no lines between one element and another, and thus is very difficult to read. I want a function that formats the above string:

<?xml version='1.0'?>
<response>
<error code='1'> Success</error>
</response>

Without resorting to manually write the format function myself, is there any .Net library or code snippet that I can use offhand?

A: 

if you load up the XMLDoc I'm pretty sure the .ToString() function posses an overload for this.

But is this for debugging? The reason that it is sent like that is to take up less space (i.e stripping unneccessary whitespace from the XML).

Spence
+1  A: 

Check the following link: How to pretty-print XML

The method in the link takes an XML string as an argument and returns a well-formed (indented) XML string.

I just copied the sample code from the link to make this answer more comprehensive and convenient.

public static String PrettyPrint(String XML)
{
    String Result = "";

    MemoryStream MS = new MemoryStream();
    XmlTextWriter W = new XmlTextWriter(MS, Encoding.Unicode);
    XmlDocument D   = new XmlDocument();

    try
    {
     // Load the XmlDocument with the XML.
     D.LoadXml(XML);

     W.Formatting = Formatting.Indented;

     // Write the XML into a formatting XmlTextWriter
     D.WriteContentTo(W);
     W.Flush();
     MS.Flush();

     // Have to rewind the MemoryStream in order to read
     // its contents.
     MS.Position = 0;

     // Read MemoryStream contents into a StreamReader.
     StreamReader SR = new StreamReader(MS);

     // Extract the text from the StreamReader.
     String FormattedXML = SR.ReadToEnd();

     Result = FormattedXML;
    }
    catch (XmlException)
    {
    }

    MS.Close();
    W.Close();

    return Result;
}
Chansik Im
+4  A: 

Use XmlTextWriter...

public static String PrintXML(String XML)
{
String Result = "";

MemoryStream mStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mStream, Encoding.Unicode);
XmlDocument document   = new XmlDocument();

try
{
 // Load the XmlDocument with the XML.
 document.LoadXml(XML);

 writer.Formatting = Formatting.Indented;

 // Write the XML into a formatting XmlTextWriter
 document.WriteContentTo(writer);
 writer.Flush();
 mStream.Flush();

 // Have to rewind the MemoryStream in order to read
 // its contents.
 mStream.Position = 0;

 // Read MemoryStream contents into a StreamReader.
 StreamReader sReader = new StreamReader(mStream);

 // Extract the text from the StreamReader.
 String FormattedXML = sReader.ReadToEnd();

 Result = FormattedXML;
}
catch (XmlException)
{
}

mStream.Close();
writer.Close();

return Result;
}
S M Kamran
A: 

You will have to parse the content somehow ... I find using LINQ the most easy way to do it. Again, it all depends on your exact scenario. Here's a working example using LINQ to pretty-print your sample XML string.

class Program
{
    static void Main(string[] args)
    {
        string xml = @"<?xml version='1.0'?><response><error code='1'> Success</error></response>";

        XDocument doc = XDocument.Parse(xml);
        Console.WriteLine(doc.ToString());
    }
}

[using statements are ommitted for brevity]

Charles Prakash Dasari