When using XMLTextReader, how to move to or search for a specific Element and Attribute, instead of

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, When using XMLTextReader, how to move to or search for a specific Element and Attribute, instead of looping through all the elements. Can you show a code example with explanation...<br/>
I am concerned that if the XML file is very big, looping through all its elements, could take a long time. If one could just move to the element of interest, then it could be faster. Jut like in case of Indexed field in a Database Table..<br/>
Thanks<br/>
Here is the Code (loop method) i got from microsofts support site..
<span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small
XmlTextReader reader = new XmlTextReader("Test.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
while (reader.MoveToNextAttribute()) // Read the attributes.
Console.Write(" " + reader.Name + "=" + reader.Value + "");
Console.WriteLine( ");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine( ");
break;
}
}
<hr class="sig diana4

View the full article
 
Back
Top