从文件加载时XDocument.Load是瞬时的,但从XMLNodeReader

时间:2018-06-07 17:57:25

标签: c# .net xml

我对此问题有点困惑。使用磁盘中的文件运行XDocument.Load时,调用将在几毫秒内完成。但是如果我们使用相同的数据但是从XMLNodeReader拨打电话,则需要超过6分钟。任何帮助,将不胜感激。谢谢! 请参阅以下代码:

XDcoument.Load from File

private static string SerializeData(Data data)
{
    var serializer = new XmlSerializer(typeof(Data));
    var dataXmlDocument = new XmlDocument();

    using (XmlWriter writer = dataXmlDocument.CreateNavigator().AppendChild())
    {
        serializer.Serialize(writer, data);
    }

    dataXmlDocument.Save("C:/test.xml");
    var dataXDocument = new XDocument();
    dataXDocument = XDocument.Load(File.OpenRead("C:\test.xml");
}

来自XMLNodeReader的XDocument.Load

private static string SerializeData(Data data)
{
    var serializer = new XmlSerializer(typeof(Data));
    var dataXmlDocument = new XmlDocument();

    using (XmlWriter writer = dataXmlDocument.CreateNavigator().AppendChild())
    {
        serializer.Serialize(writer, data);
    }

    var dataXDocument = new XDocument();
    using (var nodeReader = new XmlNodeReader(dataXmlDocument))
    {
        nodeReader.MoveToContent();
        dataXDocument = XDocument.Load(nodeReader);
    }
}

0 个答案:

没有答案