1

on my project i have a form in which i have TextBox in some point of executing my program i want to put XML string in my TextBox - nothing complicated.
Problem lies in the format in which this XML is being displayed in TextBox which is:

  <?xml version="1.0" encoding="utf-16" ?><IOTPMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Element1>value</Element1><Element2>value</Element2><Element3>value</Element3></IOTPMessage>

I want it to look like proper XML look like (with new lines, tabulators) which is easy to read :

<?xml version="1.0" encoding="utf-16" ?>
<IOTPMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Element1>value</Element1>
      <Element2>value</Element2>
      <Element3>value</Element3>
</IOTPMessage>

The way i put XML string to textBox:

TextBox someBox;
someBox.Append(XMLstring);

To be honest i don't even knew where to start - besides looking for proper articles or samples which i didn't find, maybe anyone of you can redirect me to somewhere where i can find solution?

Grzzzzzzzzzzzzz
  • 1,301
  • 4
  • 20
  • 33
  • How are you loading the xml string? – Ravi Y Jan 03 '13 at 10:26
  • I am serializing the Object to XML using method `GetXMLFromObject(Object);` which return XMLstring – Grzzzzzzzzzzzzz Jan 03 '13 at 10:28
  • Here is an example on how to "beautify" XML documents in C#: http://stackoverflow.com/questions/203528/what-is-the-simplest-way-to-get-indented-xml-with-line-breaks-from-xmldocument Maybe this helps? – Lennart Jan 03 '13 at 10:41
  • Lennart, thank you it looks good, but i will be able to test it as soon as I install VisualStudio on my mobile computer – Grzzzzzzzzzzzzz Jan 03 '13 at 10:49

1 Answers1

3

You may use XmlTextWriter in order to show proper XML Message like here:

MemoryStream w = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(w, Encoding.Unicode);

XmlDocument document = new XmlDocument();
document.LoadXml(xmlString);
writer.Formatting = Formatting.Indented;
document.WriteContentTo(writer);

writer.Flush();
w.Seek(0L, SeekOrigin.Begin);

StreamReader reader = new StreamReader(w);
return reader.ReadToEnd();
Mithrand1r
  • 2,313
  • 9
  • 37
  • 76