我有一个从文件中读入的 XMLDocument。该文件是 Unicode,并具有换行符“\n”。当我写回 XMLDocument 时,它有换行符“\r\n”。
这是代码,非常简单:
XmlTextWriter writer = new XmlTextWriter(indexFile + ".tmp", System.Text.UnicodeEncoding.Unicode);
writer.Formatting = Formatting.Indented;
doc.WriteTo(writer);
writer.Close();
XmlWriterSettings 有一个属性 NewLineChars,但我无法在“writer”上指定设置参数,它是只读的。
我可以创建具有指定 XmlWriterSettings 属性的 XmlWriter,但 XmlWriter 没有格式化属性,导致文件根本没有换行符。
所以,简而言之,我需要编写一个带有换行符 '\n' 和 Formatting.Indented 的 Unicode Xml 文件。想法?
最佳答案
我认为你很接近。您需要从设置对象创建编写器:
(从 XmlWriterSettings MSDN 页面提取)
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.NewLineOnAttributes = true;
writer = XmlWriter.Create(Console.Out, settings);
writer.WriteStartElement("order");
writer.WriteAttributeString("orderID", "367A54");
writer.WriteAttributeString("date", "2001-05-03");
writer.WriteElementString("price", "19.95");
writer.WriteEndElement();
writer.Flush();
关于c# - 将 XMLDocument 写入具有特定换行符的文件 (c#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2700435/