Introduction
XML (eXtensible Markup Language) is a widely used format for structuring and storing data in a hierarchical manner. It consists of elements enclosed in tags, which can have attributes and contain text or other elements. However, processing large XML files can be memory-intensive, especially if you load the entire document into memory at once.
Use XmlTextReader to parse large XML documents
using System.Xml;
public void FindParticularNodesUsingTextReader()
{
string xmlFilePath = @"C:\Document and Settings\Administrator\Desktop\sampleXmlDoc.xml";
using (XmlTextReader txtReader = new XmlTextReader(xmlFilePath))
{
txtReader.WhitespaceHandling = WhitespaceHandling.None;
while (txtReader.Read())
{
if (txtReader.Name.Equals("TotalPrice") && txtReader.IsStartElement())
{
txtReader.Read();
richTextBox1.AppendText(txtReader.Value);
}
}
}
}
Output
12.36 11.99 7.97
Faster, read-only XPath query-based access to data, use XPathDocument and XPathNavigator along with xpath query.
using System.Xml.XPath;
public void FindTagsUsingXPathNavigatorAndXPathDocumentNew()
{
string xmlFilePath = @"C:\Documents and Settings\Administrator\Desktop\sampleXmlDoc.xml";
XPathDocument xpDoc = new XPathDocument(xmlFilePath);
XPathNavigator xpNav = xpDoc.CreateNavigator();
XPathExpression xpExpression = xpNav.Compile("/Orders/Order/TotalPrice");
XPathNodeIterator xpIter = xpNav.Select(xpExpression);
while (xpIter.MoveNext())
{
richTextBox1.AppendText(xpIter.Current.Value);
}
}
Output
12.36 11.99 7.97
Combining XmlReader and XmlDocument. On the XmlReader, use the MoveToContent and Skip methods to skip unwanted items.
using System.Xml;
public void UseXmlReaderAndXmlDocument()
{
string xmlFilePath = @"C:\Documents and Settings\Administrator\Desktop\sampleXmlDoc.xml";
using (XmlReader rdrObj = XmlReader.Create(xmlFilePath))
{
while (rdrObj.Read())
{
if (rdrObj.NodeType.Equals(XmlNodeType.Element) &&
rdrObj.Name.Equals("TotalPrice") &&
rdrObj.IsStartElement())
{
rdrObj.Read();
richTextBox1.AppendText(rdrObj.Value);
}
}
}
}
Output
12.36 11.99 7.97
using System.Xml;
public void UseXmlReaderAndXmlDocumentNew()
{
string xmlFilePath = @"C:\Documents and Settings\Administrator\Desktop\sampleXmlDoc.xml";
using (XmlReader rdrObj = XmlReader.Create(xmlFilePath))
{
XmlDocument xmlDocObj = new XmlDocument();
while (rdrObj.Read())
{
if (rdrObj.NodeType == XmlNodeType.Element &&
rdrObj.Name.Equals("TotalPrice") &&
rdrObj.IsStartElement())
{
rdrObj.Read();
richTextBox1.AppendText(rdrObj.Value);
}
}
rdrObj.Close(); // Close the XmlReader before loading into XmlDocument
xmlDocObj.Load(xmlFilePath);
richTextBox1.Text = xmlDocObj.InnerText;
}
}

Liju GopalanPosted May 29, 2012, 8:10 AM
Hi Roua, This sample explains about , different ways to extract contents from an XML Document. An Xml doc can hold float values as it will be treated as string internally. So I guess you need to typecast it before start processing . Please share the portion of the code and sample xml , if you have not able to resolve the issue . Have a great day ahead, liju
roua ammerPosted Mar 29, 2012, 4:30 AM
hi Dr.LIJU i realy benefit from your example and i implementation it,but i have a problem.i work in enterprise architect tool ,i make a diagrams for many project management process and in this diagrams(classes) i needs a float value for many many attribute (ex.cost=43%,,,bad schedule =65%......),i take this value and use it in mathematical relations(fuzzy set ).when i use your example i can not get it .could you help me please
Swapnil KaduPosted Nov 5, 2009, 4:14 AM
Hi Liju, I gone throught the post. Its very nice. but I am facing a problem with xmltextreader and xmltextwriter class. these classes are not found even though i gave a reference of system.xml do u know how to overcome this problem. Help appreciate. Thank you, swapnil