Guest User

Untitled

a guest
May 27th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. <?xml version='1.0'?><response><error code='1'> Success</error></response>
  2.  
  3. <?xml version='1.0'?>
  4. <response>
  5. <error code='1'> Success</error>
  6. </response>
  7.  
  8. public static String PrintXML(String XML)
  9. {
  10. String Result = "";
  11.  
  12. MemoryStream mStream = new MemoryStream();
  13. XmlTextWriter writer = new XmlTextWriter(mStream, Encoding.Unicode);
  14. XmlDocument document = new XmlDocument();
  15.  
  16. try
  17. {
  18. // Load the XmlDocument with the XML.
  19. document.LoadXml(XML);
  20.  
  21. writer.Formatting = Formatting.Indented;
  22.  
  23. // Write the XML into a formatting XmlTextWriter
  24. document.WriteContentTo(writer);
  25. writer.Flush();
  26. mStream.Flush();
  27.  
  28. // Have to rewind the MemoryStream in order to read
  29. // its contents.
  30. mStream.Position = 0;
  31.  
  32. // Read MemoryStream contents into a StreamReader.
  33. StreamReader sReader = new StreamReader(mStream);
  34.  
  35. // Extract the text from the StreamReader.
  36. String FormattedXML = sReader.ReadToEnd();
  37.  
  38. Result = FormattedXML;
  39. }
  40. catch (XmlException)
  41. {
  42. }
  43.  
  44. mStream.Close();
  45. writer.Close();
  46.  
  47. return Result;
  48. }
  49.  
  50. public static String PrettyPrint(String XML)
  51. {
  52. String Result = "";
  53.  
  54. MemoryStream MS = new MemoryStream();
  55. XmlTextWriter W = new XmlTextWriter(MS, Encoding.Unicode);
  56. XmlDocument D = new XmlDocument();
  57.  
  58. try
  59. {
  60. // Load the XmlDocument with the XML.
  61. D.LoadXml(XML);
  62.  
  63. W.Formatting = Formatting.Indented;
  64.  
  65. // Write the XML into a formatting XmlTextWriter
  66. D.WriteContentTo(W);
  67. W.Flush();
  68. MS.Flush();
  69.  
  70. // Have to rewind the MemoryStream in order to read
  71. // its contents.
  72. MS.Position = 0;
  73.  
  74. // Read MemoryStream contents into a StreamReader.
  75. StreamReader SR = new StreamReader(MS);
  76.  
  77. // Extract the text from the StreamReader.
  78. String FormattedXML = SR.ReadToEnd();
  79.  
  80. Result = FormattedXML;
  81. }
  82. catch (XmlException)
  83. {
  84. }
  85.  
  86. MS.Close();
  87. W.Close();
  88.  
  89. return Result;
  90. }
  91.  
  92. class Program
  93. {
  94. static void Main(string[] args)
  95. {
  96. string xml = @"<?xml version='1.0'?><response><error code='1'> Success</error></response>";
  97.  
  98. XDocument doc = XDocument.Parse(xml);
  99. Console.WriteLine(doc.ToString());
  100. }
  101. }
Add Comment
Please, Sign In to add comment