I’m having a requirement to create a XML Document and load the same with a string. I’ve written a small test program to do the same.
string xmlString = "<Control1>" +
"\n\t<Stamp type=\"This is \n\ta test\" />" +
"\n</Control1>"
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlString);
Console.WriteLine(xmlDocument.InnerXml);
The string that I have used has multiple new line sequences "\n" (essentially used for wrapping). But after doing the above mentioned steps, the new line sequence does not hold good for the Xml Document. The output is I get is:
<Control1><Stamp type="This is 
 a test" /></Control1>
But the output I need to get is:
<Control1>
<Stamp type="This is
a te开发者_开发知识库st" />
</Control1>
Any pointers on how can I make sure that the formatting of the string is retained inside the XML Document as well.
Thanks in advance, Kunal
By default it trims out white space.... set
xmlDocument.PreserveWhitespace = true;
Read about White Space in Attributes
MSDN Documentation on XMLDocument PreserveWhitespace property
精彩评论