写的XMLDocument与特定换行符文件(C#) [英] Writing XMLDocument to file with specific newline character (c#)
问题描述
我有我从文件中读取一个XMLDocument。该文件是Unicode,并有换行符\\\
。当我写的XMLDocument退了出来,它有换行符\r\\\
I have an XMLDocument that I have read in from file. The file is Unicode, and has the newline character '\n'. When I write the XMLDocument back out, it has the newline characters '\r\n'.
下面是代码,很简单:
XmlTextWriter writer = new XmlTextWriter(indexFile + ".tmp", System.Text.UnicodeEncoding.Unicode);
writer.Formatting = Formatting.Indented;
doc.WriteTo(writer);
writer.Close();
XmlWriterSettings有一个属性,NewLineChars,但我不能指定上'作家'的设置参数,它是只读的。
XmlWriterSettings has a property, NewLineChars, but I am unable to specify the settings parameter on 'writer', it is read-only.
我可以创建具有指定XmlWriterSettings属性的XmlWriter,但的XmlWriter不具有格式属性,产生一个文件,没有换行符在所有
I can create a XmlWriter with a specified XmlWriterSettings property, but XmlWriter does not have a formatting property, resulting in a file with no linebreaks at all.
因此,简而言之,我需要编写与换行符\\\
和Formatting.Indented一个Unicode XML文件。思考?
So, in short, I need to write a Unicode Xml file with newline character '\n' and Formatting.Indented. Thoughts?
推荐答案
我觉得你很接近。您需要创建的设置Writer对象:
I think you're close. You need to create the writer from the settings object:
(从的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();
这篇关于写的XMLDocument与特定换行符文件(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!