Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version='1.0'?><response><error code='1'> Success</error></response>
- <?xml version='1.0'?>
- <response>
- <error code='1'> Success</error>
- </response>
- 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;
- }
- 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;
- }
- 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());
- }
- }
Add Comment
Please, Sign In to add comment