home
links
tips

code
users
tools
tutorials
projects
web
help
design

mudabone
suletzki
trans48
nutrition reality

MarthaAttire!

code Pretty Print XML



          public 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;
          }


Comments:

From Tim Erwin [72.17.139.210] - 8/5/05 11:56 AM

public static string PrettyPrint(string XML)

{

using( System.IO.MemoryStream MyMemoryStream = new System.IO.MemoryStream( ) )

{

System.Xml.XmlTextWriter MyWriter = new System.Xml.XmlTextWriter(MyMemoryStream, System.Text.Encoding.Unicode);

System.Xml.XmlDocument MyDocument = new System.Xml.XmlDocument();

// Load the XmlDocument with the XML.

MyDocument.LoadXml(XML);

MyWriter.Formatting = System.Xml.Formatting.Indented;

// Write the XML into a formatting XmlTextWriter

MyDocument.WriteContentTo(MyWriter);

MyWriter.Flush();

MyMemoryStream.Flush();

// Have to rewind the MemoryStream in order to read

// its contents.

MyMemoryStream.Position = 0;

// Read MemoryStream contents into a StreamReader.

using( System.IO.StreamReader MyReader = new System.IO.StreamReader(MyMemoryStream) )

{

// Extract the text from the StreamReader.

return MyReader.ReadToEnd();

}

}

}

From Scott Hungarter [216.37.191.179] - 5/19/05 10:13 AM

million!

From Scott Hungarter [216.37.191.179] - 5/19/05 10:12 AM

Thanks a


Last Modified 3/19/04 1:58 PM

Hide Tools