我有一个XML,如下所示
<root>
<node>
<a1>text</a1>
<a2>text</a2>
<a3></a3>
</node>
</root>
如果我在xmldocument中加载并保存它。 xml跟随
<root>
<node>
<a1>text</a1>
<a2>text</a2>
<a3>
</a3>
</node>
</root>
我的代码段:
public static Void Update(String Path)
{
FileStream docIn = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlDocument xml = new XmlDocument();
xml.Load(Path);
XmlNodeList nodeY = xml.SelectNodes("/root/node/a1");
_count = 0;
foreach (XmlNode live in nodeY)
{
//changing nodeY InnerText
}
xml.Save(Path);
}
我不希望使用preservewhitespace = true选项,因为它使我的xml看起来像这样
<root><node><a1>text</a1><a2>text</a2><a3></a3></node></root>
提前致谢!!!
答案 0 :(得分:1)
尝试使用实例XmlWriter
类明确指定XmlWriterSettings
设置。
XmlWriterSettings settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
Indent = true,
NewLineChars = Environment.NewLine,
NewLineHandling = NewLineHandling.Replace,
OmitXmlDeclaration = false
};
using (XmlWriter writer = XmlWriter.Create(outXmlPath, settings))
{
xml.Save(writer);
}
希望这有帮助。