1

I have a large XML which I need to parse in C#. Unfortunately, the XML Serializer class is not an option. I really need to write time-efficient code. Furthermore, I want to preserve writing inelegant XmlReader code by hand. Any ideas?

  • 1
    See my xmlreader at following posting : http://stackoverflow.com/questions/34274568/how-to-read-an-xml-file-by-using-xmlreader-in-c-sharp – jdweng Dec 20 '16 at 08:00
  • This helps. Anyways, converting to XElement does have overhead. It's kind of a hybrid approach. See here: http://stackoverflow.com/questions/14000846/xdocument-performance –  Dec 20 '16 at 08:38

2 Answers2

0

Try using XDocument, after skipping the learning curve its really easy to read/parse XML files using XDocument.

Hagay Goshen
  • 599
  • 2
  • 13
  • But it's XDocument is really slow compared to XmlReader. I really need time-efficient xml parsing code. –  Dec 20 '16 at 06:52
  • However, I think someone must already have tackled this problem. –  Dec 20 '16 at 08:34
0

So I needed to write code by hand. I chose to use a hybrid approach.

private static IEnumerable<XElement> StreamElements(string fileName, string elementName)
    {
        using (XmlReader reader = XmlReader.Create(fileName))
        {
            while (reader.Name == elementName || reader.ReadToFollowing(elementName))
            {
                yield return (XElement)XNode.ReadFrom(reader);
            }
            reader.Close();
        }
    }