How to Parse (Big) Xml in C# Code

What is the best way to parse (big) XML in C# Code?

Use XmlReader to parse large XML documents. XmlReader provides fast, forward-only, non-cached access to XML data. (Forward-only means you can read the XML file from beginning to end but cannot move backwards in the file.) XmlReader uses small amounts of memory, and is equivalent to using a simple SAX reader.

    using (XmlReader myReader = XmlReader.Create(@"c:\data\coords.xml"))
{
while (myReader.Read())
{
// Process each node (myReader.Value) here
// ...
}
}

You can use XmlReader to process files that are up to 2 gigabytes (GB) in size.

Ref: How to read XML from a file by using Visual C#

most efficient way in c# to parse a large Xml string (to expand DTD references, add new lines etc)

You can do essentially what you have in your example, but with XmlReader:

XmlReader xmlReader = ...;

using (XmlWriter xmlWriter = ...)
{
xmlWriter.WriteNode(reader, true);
}

This will be the most efficient way -- streaming the document node by node vs. reading the entire thing into memory before writing it out.

How to parse very huge XML Files in C#?

Use XML reader instead of XML dom. XML dom stores the whole file in memory which is totally useless:

http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx

C# and Reading Large XML Files

The answer to this question hasn't changed in .NET 4 - for best performance you should still be using XmlReader as it streams the document instead of loading the full thing into memory.

The code you refer to uses XmlReader for the actual querying so should be reasonably quick on large documents.

How to parse large XML files with xpaths?

I'm using SelectNodes to cut the xml document in small elements

XmlDocument doc = new XmlDocument();
XmlNodeList nodeList = doc.SelectNodes(".....");
if (nodeList != null)
{
foreach (XmlNode node in nodeList)

Efficient Parsing of XML

With large XML files you have to use an XmlReader. The code below will read one Product at a time.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create("filename");
while(!reader.EOF)
{
if (reader.Name != "Product")
{
reader.ReadToFollowing("Product");
}
if (!reader.EOF)
{
XElement product = (XElement)XElement.ReadFrom(reader);
string lastUpdated = (string)product.Element("lastUpdated");
}
}
}
}
}


Related Topics



Leave a reply



Submit