1

好的,所以我有一个方法应该读取一个 XML 文档,然后将它们全部包装在一个更大的 XML 文档中。

<Elements>
    <Element>
    <Element>
</Elements>

问题是使用输出文件下方的代码时缺少声明

<?xml >

这是什么原因?我希望这是我想念的愚蠢的东西。

XmlWriter xmlWriter = XmlWriter.Create(outputFileName, xwsSettings);

xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement("Elements");

foreach (var item in items)
{
    xmlReader = XmlReader.Create(item.FullFilename);

    while (xmlReader.Read())
    {
        if(xmlReader.NodeType == XmlNodeType.Element)
        {
            xmlWriter.WriteRaw(xmlReader.ReadOuterXml());
            break;
  }
    }
}

xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();

xmlWriter.Flush();
xmlWriter.Close();
xmlWriter = null;
4

1 回答 1

3

正如@Tim 强调检查我的 XmlWriterSettings 设置不正确

XmlWriterSettings xwsSettings = new XmlWriterSettings();
xwsSettings.CheckCharacters = false;
xwsSettings.CloseOutput = true;
xwsSettings.ConformanceLevel = ConformanceLevel.Document;
xwsSettings.Encoding = Encoding.UTF8;
xwsSettings.Indent = false;
xwsSettings.NewLineHandling = NewLineHandling.None;

xwsSettings.OmitXmlDeclaration = true; <---- should have been false;
于 2013-08-07T10:25:33.313 回答