简体   繁体   English

使用 XMLWriter WriteNode 将 XMLReader 转换为 XML 文件 - 对于大型 XML 非常慢

[英]XMLReader to XML file using XMLWriter WriteNode - Very Slow for Large XML

In C# I use SqlCommand ExecuteXmlReader() to call a SQL Server stored procedure which uses 'For XML' to return large (1gb+) complex XML files (multiple hierarchy).在 C# 中,我使用SqlCommand ExecuteXmlReader()调用 SQL Server 存储过程,该过程使用“For XML”返回大型 (1gb+) 复杂 XML 文件(多层次结构)。

ExecuteXmlReader() returns an XmlReader which I wish to save to an XML file. ExecuteXmlReader()返回一个XmlReader ,我希望将其保存到 XML 文件中。 To do this I use an XmlWriter to stream the data from the XMLReader to the file system.为此,我使用XmlWriter将数据从XMLReader流式传输到文件系统。

using (XmlReader xmlFromDatabase = xmlReaderFromDatabase)
{
  var settings = new XmlWriterSettings {Encoding = Encoding.UTF8, Indent = true};
  using (XmlWriter outputXmlFileToDisk = XmlWriter.Create(fileDirectory + fileName, settings))
  {
    outputXmlFileToDisk.WriteNode(xmlFromDatabase, false);
  }
}

Side note: I can't load the entire XML into memory ( XDocument ) as it is too large.旁注:我无法将整个 XML 加载到内存 ( XDocument ) 中,因为它太大了。

My problem is that the WriteNode is very slow - it is taking hours to write the file.我的问题是WriteNode非常慢 - 写入文件需要几个小时。 If I kill my application the XML file written on the disc is partially written since the file is being streamed out to node by node.如果我终止我的应用程序,则写入磁盘的 XML 文件会被部分写入,因为该文件正在逐节点流式传输到节点。

Is there a better way to save the XML faster from a XmlReader than a XMLWriter WriteNode ?有没有比XMLWriter WriteNode更快地从XmlReader保存 XML 的XMLWriter WriteNode

(I know there is .ReadInnerXml() but this returns a string which is not good for the size of the XML) (我知道有 .ReadInnerXml() 但这会返回一个不适合 XML 大小的字符串)

After I export the file is need to transform it (I may use Saxon as the .net framework hasn't proved as performant as I'd like) and schema validate it through C#.导出文件后,需要对其进行转换(我可能会使用 Saxon,因为 .net 框架没有证明我想要的性能)并通过 C# 模式验证它。

I found the solution, using XmlReader / XMLWriter seems to be a good approach to take ( https://msdn.microsoft.com/en-us/library/ff647804.aspx ) just that the XMLWriter-WriteNode by default validates the XML as it writes.我找到了解决方案,使用XmlReader / XMLWriter似乎是一个很好的方法( https://msdn.microsoft.com/en-us/library/ff647804.aspx )只是默认情况下 XMLWriter-WriteNode 将 XML 验证为它写道。 Since we know the XML returned from SQL Server is XML valid and we also XSD validate all XML before sending it we don't need to have the writer perform the validation as it writes.由于我们知道从 SQL Server 返回的 XML 是 XML 有效的,并且我们还在发送之前 XSD 验证所有 XML,因此我们不需要让编写器在写入时执行验证。 Passing the Settings object to the XMLWriter with CheckCharacters = false prevents the redundant validation and outputs the file in just a few minutes as apposed to hours.使用CheckCharacters = false将 Settings 对象传递给XMLWriter可以防止冗余验证,并在几分钟内输出文件,而不是几小时。

var settings = new XmlWriterSettings{
            CheckCharacters = false,
            NewLineHandling = NewLineHandling.None,
            Indent = true,
            Encoding = Encoding.UTF8 };

using (var outputXmlFileToDisk = XmlWriter.Create(fileDirectory + fileName, settings))
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM