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
How Do the Major C# Di/Ioc Frameworks Compare
When Is It Better to Use String.Format VS String Concatenation
Format Excel Column to Decimal Doing Export from C#
C# Open a New Form Then Close the Current Form
Why Can't I Have Abstract Static Methods in C#
Most Efficient Way to Remove Special Characters from String
.Net Out of Memory Exception - Used 1.3Gb But Have 16Gb Installed
Notify Observablecollection When Item Changes
General Purpose Fromevent Method
What How to Use for Good Quality Code Coverage for C#/.Net
C# Member Variable Initialization; Best Practice
C# Object Pooling Pattern Implementation
Word Wrap for a Label in Windows Forms
How to Compile and Execute New Code at Runtime in .Net