我正在使用XmlDocument处理xml。
我如何保存我的“XmlDocument”与当前的格式相同?
当前格式:
<?xml version="1.0" encoding="utf-8"?>
<root>
<element></element>
</root>
代码:
XmlDocument testDoc = new XmlDocument();
testDoc.Load(@"C:\Test.xml");
**(do reading/writing using only XmlDocument methods)**
testDoc.Save(@"C:\Test.xml");
有一个类似的主题:XmlDocument class is removing formatting, c#, .NET 被接受的答案是PreserveWhiteSpace = true,但实际上它会删除所有空格而不是保留它们。
例子:
代码:
XmlDocument testDoc = new XmlDocument();
testDoc.Load(@"C:\Test.xml");
testDoc.PreserveWhitespace = true;
testDoc.Save(@"C:\Test.xml");
结果:
<?xml version="1.0" encoding="utf-8"?><root><element></element></root>
PreserveWhiteSpace = true
,实际上会删除所有空格而不是保留它们。你确定吗?MSDN说:如果在调用Load或LoadXml之前PreserveWhitespace
为true,则会保留空格节点;否则,如果此属性为false,则保留重要的空格,但不保留空格。 - Haris Hasan