Reading and Parsing XML Nodes – C#
filed in C# on Feb.02, 2010
The XmlTextReader provides a forward only mean of reading a specific XML node. The application reads each node of an XML document, determining along the way whether the current node is what is needed. This is typically accomplished by constructing an XmlTextReader object and then iteratively calling—within a loop—the XmlTextReader::Read method until that method returns false.
i.e:
try
{
XmlTextReader* xmlReader = new XmlTextReader(xmlFileName);
while (xmlReader->Read())
{
// parse here
}
xmlReader.close();
}
catch (Exception err)
{
// Exception
}
__finally
{
}
As each call to the Read method will read the next node in the XML file, your code must be able to distinguish between node types. This includes the XML file’s opening declaration node to element and text nodes and includes special nodes for comments and whitespace. The XmlTextReader::NodeType property is an enum of type XmlNodeType that indicates the exact type of the node being read.
Leave a Reply