Reading Elements
The following table describes the methods and properties that the XmlReader class provides for processing elements. After the XmlReader is positioned on an element, the node properties, such as Name, reflect the element values. In addition to the members described below, any of the general methods and properties of the XmlReader class can also be used to process elements. For example, you can use the ReadInnerXml method to read the contents of an element.
Note
These methods include an initial call to the MoveToContent method.
Member name |
Description |
---|---|
Checks if the current node is a start tag or an empty element tag. |
|
Checks that the current node is an element and advances the reader to the next node. |
|
Checks that the current node is an end tag and advances the reader to the next node. |
|
Reads a text-only element. |
|
Advances the XmlReader to the next descendant element with the specified name. |
|
Advances the XmlReader to the next sibling element with the specified name. |
|
Checks if the current element has an empty element tag. This property enables you to determine the difference between the following:
In other words, IsEmptyElement simply reports whether or not the element in the source document has an end element tag. |
Examples
The following code reads elements using the ReadStartElement and ReadString methods.
Using reader As XmlReader = XmlReader.Create("book3.xml")
' Parse the XML document. ReadString is used to
' read the text content of the elements.
reader.Read()
reader.ReadStartElement("book")
reader.ReadStartElement("title")
Console.Write("The content of the title element: ")
Console.WriteLine(reader.ReadString())
reader.ReadEndElement()
reader.ReadStartElement("price")
Console.Write("The content of the price element: ")
Console.WriteLine(reader.ReadString())
reader.ReadEndElement()
reader.ReadEndElement()
End Using
using (XmlReader reader = XmlReader.Create("book3.xml")) {
// Parse the XML document. ReadString is used to
// read the text content of the elements.
reader.Read();
reader.ReadStartElement("book");
reader.ReadStartElement("title");
Console.Write("The content of the title element: ");
Console.WriteLine(reader.ReadString());
reader.ReadEndElement();
reader.ReadStartElement("price");
Console.Write("The content of the price element: ");
Console.WriteLine(reader.ReadString());
reader.ReadEndElement();
reader.ReadEndElement();
}
The following code processes elements by using a While loop.
While reader.Read()
If reader.IsStartElement() Then
If reader.IsEmptyElement Then
Console.WriteLine("<{0}/>", reader.Name)
Else
Console.Write("<{0}> ", reader.Name)
reader.Read() ' Read the start tag.
If reader.IsStartElement() Then ' Handle nested elements.
Console.Write(vbCr + vbLf + "<{0}>", reader.Name)
End If
Console.WriteLine(reader.ReadString()) 'Read the text content of the element.
End If
End If
End While
while (reader.Read()) {
if (reader.IsStartElement()) {
if (reader.IsEmptyElement)
Console.WriteLine("<{0}/>", reader.Name);
else {
Console.Write("<{0}> ", reader.Name);
reader.Read(); // Read the start tag.
if (reader.IsStartElement()) // Handle nested elements.
Console.Write("\r\n<{0}>", reader.Name);
Console.WriteLine(reader.ReadString()); //Read the text content of the element.
}
}
}