2

I have this code on my program that actually loads 500 MB and up files.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
reader.Close();

I get this kind of error and don't know how to resolve the problem. Please send me some advice.

3
  • 2
    You may need to switch to a stream based XML parser. Commented Apr 5, 2011 at 1:33
  • 1
    And/or switch to actually loading smaller XML files. Commented Apr 5, 2011 at 1:36
  • @Matt can you show me some codes on how to do that? Commented Apr 5, 2011 at 2:01

2 Answers 2

4

I would use an XmlReader to parse the document, providing forward only access to the data and cleans itself up nicely in memory -- of course, it can be much more complex without the convenience of the XmlDocument class.

This simple sample will start by starting to read the file line by line, providing an XmlReader for each line.

using (var rdr = XmlReader.Create(new StreamReader("File.xml")))
{
    while (rdr.Read())
    {
        //do what you will with the line
    }
}

See the methods and properties available to you when using the XmlReader at XmlReader Properties (MSDN)

Sign up to request clarification or add additional context in comments.

2 Comments

in addition to the above codes I have the following: XmlTextReader myTextReader = new XmlTextReader(txtFileName.Text); myTextReader.WhitespaceHandling = WhitespaceHandling.None; while (myTextReader.Read()) { foreach (XmlNode node in xmlDoc) { TreeNode treeNode = XmlNode2TreeNode(myTextReader, myTextReader.p); treeNode.Text = FormatName(node); treeView1.Nodes.Add(treeNode); } }
added rudimentary sample to answer, mingzcky.
0

you need something like SAX but for .NET.

http://sourceforge.net/projects/saxdotnet/ or the XmlReader, basically a stream based parser.

HTH

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.