Accessing and Manipulating XML Data in Microsoft .NET Framework
by Team uCertifyuCertify.com
Wednesday, 23rd August 2006
The XmlReader Class
The XmlReader class is an abstract class that provides fast, non-cacheable, forward only, and read-only access to XML data. The methods of the XmlReader class provide access to the elements and attributes of XML data. The XmlTextReader class derived from the XmlReader class implements the methods defined by the XmlReader class. The XmlValidating Reader is another class derived from the XmlReader class facilitates to read XML data and supports the document type definition (DTD) or schema validation.
The XmlTextReader class requires fast access to XML data, and therefore is not required to read the entire document into memory via the Document Object Model or DOM. The following are some of the properties and methods of the XmlTextReader class:
- AttributeCount Property Returns an integer value that specifies the number of attributes on the current node.
- HasAttributes Property Returns true determining whether a node contains attributes.
- HasValue Property Returns true determining whether the current node contains a value.
- Depth Property Returns an integer value that specifies the depth of the current node in an XML document.
- IsEmptyElement Property Returns true specifying whether the current node of the XML element is empty.
- Item Property Specifies a string value to an attribute.
- Value Property Specifies the value of the current node as text.
- Name Property Specifies the name of the current node in an XML document.
- IsStartElement() Method Checks whether the current element is a start element.
- Read() Method Reads the next node from the XML document.
- ReadAttributeValue() Method Reads the attribute value.
- ReadString() Method Reads the text content of an element or node.
- MoveToFirstAttribute() Method Moves the current node to the first attribute.
- MoveToNextAttribute() Method Moves the current node to the next attribute.
The following code snippet written in Visual Basic .NET shows how to read data using the XmlTextReader class:
While readdata.Read()
Select Case readdata.NodeType
Case XmlNodeType.Element
Console.Write("<" + readdata.Name)
While readdata.MoveToNextAttribute() Console.Write(" " & readdata.Name & "= ' " & _ readdata.Value & "' ")
End While
Console.Write(">")
If readdata.HasAttributes Then
While readdata.MoveToNextAttribute
Console.Write(" " & readdata.Value & " ")
End While
End If
Case XmlNodeType.Text
Console.Write(readdata.Value)
Case XmlNodeType.EndElement
Console.WriteLine(("</" & readdata.Name & ">"))
End Select
End While
The following code snippet written in Visual C# .NET shows how to read data using the XmlTextReader class:
while (readdata.Read())
{
switch (readdata.NodeType)
{
case XmlNodeType.Element:
Console.Write("<" + readdata.Name);
while (readdata.MoveToNextAttribute())
{
Console.Write(" " + readdata.Name + "= '" + readdata.Value + " '");
}
Console.Write(">");
if (readdata.HasAttributes)
{
while (readdata.MoveToNextAttribute)
{
Console.Write(" " + readdata.Value + " ");
}
}
break;
case XmlNodeType.Text:
Console.Write(readdata.Value);
break;
case XmlNodeType.EndElement:
Console.WriteLine(("</" + readdata.Name + ">"));
break;
}
}
Options:
Printer Friendly
Email Friend
uCertify was formed in 1996 with an aim to offer high quality educational training software and services in the field of information technology to its customers. uCertify provides exam preparation solutions for the certification exams of Microsoft, CIW, CompTIA, Oracle, Sun and other leading IT vendors. To know more about uCertify, please visit http://www.ucertify.com/
