Parsing XML file using C#? Parsing XML file using C#? xml xml

Parsing XML file using C#?


8MB really isn't very large at all by modern standards. Personally I'd use LINQ to XML:

XDocument doc = XDocument.Load("ex.xml");var projects = doc.Descendants("proj_title")                  .Where(x => (string) x == "heat_run")                  .Select(x => x.Parent) // Just for simplicity                  .Select(x => new {                              Start = (int) x.Element("proj_start"),                              End = (int) x.Element("proj_end")                          });foreach (var project in projects){    Console.WriteLine("Start: {0}; End: {1}", project.Start, project.End);}

(Obviously adjust this to your own requirements - it's not really clear what you need to do based on the question.)

Alternative query:

var projects = doc.Descendants("proj")                  .Where(x => (string) x.Element("proj_title") == "heat_run")                  .Select(x => new {                              Start = (int) x.Element("proj_start"),                              End = (int) x.Element("proj_end")                          });


You can use XPath to find all nodes that match, for example:

XmlNodeList matches = xmlDoc.SelectNodes("proj[proj_title='heat_run']")

matches will contain all proj nodes that match the critera. Learn more about XPath: http://www.w3schools.com/xsl/xpath_syntax.asp

MSDN Documentation on SelectNodes


Use XDocument and use the LINQ api.http://msdn.microsoft.com/en-us/library/bb387098.aspx

If the performance is not what you expect after trying it, you have to look for a sax parser.A Sax parser will not load the whole document in memory and try to apply an xpath expression on everything in memory. It works more in an event driven approach and in some cases this can be a lot faster and does not use as much memory.

There are probably sax parsers for .NET around there, haven't used them myself for .NET but I did for C++.