开发者

XML Document Wrapping

开发者 https://www.devze.com 2023-01-12 06:16 出处:网络
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.

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 &#xA; 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

0

精彩评论

暂无评论...
验证码 换一张
取 消