1

I am a bit stuck with this issue. When running XDocument.Load with a file from disk, the call completes within milliseconds. But if we make the call using the same data but from a XMLNodeReader, it takes upwards of 6 minutes. Any help would be appreciated. Thanks! See the code below:

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");
}

XDocument.Load from XMLNodeReader

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);
    }
}
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
  • 1) Consider rewriting your code to use `XDocument` and nothing else. That will result in the best performance since you'll never need to convert to and from the old `XmlDocument`. See e.g. [Serialize an object to XElement and Deserialize it in memory](https://stackoverflow.com/a/28872669/3744182) or [How do I serialize an object into an XDocument?](https://stackoverflow.com/q/806095). 2) Can you share a [mcve] or at least the results of profiling? As it is we can only guess the problem, since there's nothing blatantly obviously wrong with your code. – dbc Jun 07 '18 at 20:22
  • 3) That being said, just a wild shot in the dark here, but does `ProperXmlNodeReader` from [Deserialize object property with StringReader vs XmlNodeReader](https://stackoverflow.com/a/30115691/3744182) improve things? – dbc Jun 07 '18 at 20:23
  • @dbc 1) I will attempt this. 2) I could, but it would take more time out of my work day to scrub out the sensitive parts than it is worth. 3) If 1) doesn't work, I will attempt this. Thanks for the reply, I appreciate the help! – scrapmetal134 Jun 08 '18 at 11:37

0 Answers0