当我使用XmlReader.ReadOuterXml()时,元素之间以\n分隔而不是\r\n。例如,如果我有一个XmlDocument表征
<A>
<B>
</B>
</A>
我收到了
<A>\n<B>\n</B>\n</A>
是否有指定换行符的选项?XmlWriterSettings 有,但 XmlReader 似乎没有。
这是我读取 XML 的代码。注意,默认情况下,XmlWriterSettings 具有 NewLineHandling = Replace。
XmlDocument xmlDocument = <Generate some XmlDocument>
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
// Use a memory stream because it accepts UTF8 characters. If we use a
// string builder the XML will be UTF16.
using (MemoryStream memStream = new MemoryStream())
{
using (XmlWriter xmlWriter = XmlWriter.Create(memStream, settings))
{
xmlDocument.Save(xmlWriter);
}
//Set the pointer back to the beginning of the stream to be read
memStream.Position = 0;
using (XmlReader reader = XmlReader.Create(memStream))
{
reader.Read();
string header = reader.Value;
reader.MoveToContent();
return "<?xml " + header + " ?>" + Environment.NewLine + reader.ReadOuterXml();
}
}