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.
These methods include an initial call to the MoveToContent method.
Member name
Description
IsStartElement
Checks if the current node is a start tag or an empty element tag.
ReadStartElement
Checks that the current node is an element and advances the reader to the next node.
ReadEndElement
Checks that the current node is an end tag and advances the reader to the next node.
ReadElementString
Reads a text-only element.
ReadToDescendant
Advances the XmlReader to the next descendant element with the specified name.
ReadToNextSibling
Advances the XmlReader to the next sibling element with the specified name.
IsEmptyElement
Checks if the current element has an empty element tag. This property enables you to determine the difference between the following:
<item num="123"/> (IsEmptyElement is true.)
<item num="123"> (IsEmptyElement is false, although element content is empty.)
In other words, IsEmptyElement simply reports whether or not the element in the source document has an end element tag.
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. } } }
If you are wondering what is books.xml then do create a xml file like below:
<book>
<title>C# Introduction</title>
<price>20</price>
</book>
<# .SYNOPSIS This script reads a simple XML file and prints the results .DESCRIPTION This script is a re-write of an XML C# MSDN Sample. .NOTES File Name : get-xmlfilecontents.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 Books3.xml = <book> <title>C# Introduction</title> <price>20</price> </book> .LINK This script posted to: http://www.pshscripts.blogspot.com MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/t9bfea29.aspx .EXAMPLE PSH [C:\foo]: .\Get-XMLFileContents.PS1 The content of the title element: C# Introduction The content of the price element: 20 #> ## # Start of Script ## # Create XML Reader $reader = [system.Xml.XmlReader]::Create("C:\foo\book3.xml") # Parse the XML document. ReadString is used to # read the text content of the elements. $result=$reader.Read() # Read/Display title $reader.ReadStartElement("book") $reader.ReadStartElement("title") "The content of the title element: {0}" -f $reader.ReadString() $reader.ReadEndElement() # Read and display price $reader.ReadStartElement("price") "The content of the price element: {0}" -f $reader.ReadString() $reader.ReadEndElement() $reader.ReadEndElement() # End of Script