1

I have a very large xml-file (let's say it has about 300000 elements). In my part of the application I only have to know if the name of the root-element is ApplicationLog and if there is an attribute called LogId in the root-element.

To read the XML I use:

XDocument document;
using (StreamReader streamReader = new StreamReader(filePath, true))
{   
    document = XDocument.Load(streamReader);
}

and to get the information I need:

try
{
    if (document.Root != null)
    {       
        if (string.Equals(document.Root.Name.LocalName, "ApplicationLog", StringComparison.InvariantCultureIgnoreCase) &&
            document.Root.HasAttributes && (from o in document.Root.Attributes() where string.Equals(o.Name.LocalName, "LogId", StringComparison.InvariantCultureIgnoreCase) select o).Any())
        {
            isRelevantFile = true;
        }
    }
}
catch (Exception e)
{
}

This just works fine.

The problem is that the XDocument.Load takes about 15 seconds to load a XML-File which is about 20MB.

I also tried it with the XmlDocument, but there I have the same problem.

My first idea for a solution was to read the file as text and parse the first lines for the searched element/attribute. But this seems to be not so professional to me.

Does anybody know a better way to achieve this?

Tomtom
  • 9,087
  • 7
  • 52
  • 95

2 Answers2

4

Use the XmlReader API with

using (XmlReader xr = XmlReader.Create(filePath))
{
  xr.MoveToContent();
  if (xr.LocalName == "ApplicationLog" ...)

}
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

You can try the solution provided here or use/develop a SAX reader such as this one. You can find more information on SAX here.

Community
  • 1
  • 1
user3021830
  • 2,784
  • 2
  • 22
  • 43