Sd.xml file:
<?xml version= "1.0" encoding= "gb2312"?>
<!--This is an XML file-->
<xml1>
<item name= "1" > First item</item>
<item name= "2" >
<item name= "1" > This node (1) </item>
</item>
</xml1>
CS Operation Code:
protected void Page_Load (object sender, EventArgs e)
{
XmlTextReader reader = null;
Try
{
reader = new XmlTextReader (Server.MapPath ("Sd.xml"));
Reader. WhitespaceHandling = WhitespaceHandling.None;
while (reader. Read ())
{
Switch (reader. NodeType)
{
Case XmlNodeType.Element:
if (reader. Name = = "Item" && reader. HasAttributes)
{
Response.Write ("Attributes Value:" + reader.) GetAttribute (0) + "<br>"); Property starts with an index of 0
}
Break
Case XmlNodeType.Text:
Response.Write ("Text:" + reader.) Value + "<br>");
Break
Case Xmlnodetype.cdata:
Response.Write ("CDATA:" + reader.) Value + "<br>");
Break
Case Xmlnodetype.processinginstruction:
Response.Write ("ProcessingInstruction:" + reader.) Name + "-----" + reader. Value + "<br>");
Break
Case Xmlnodetype.comment:
Response.Write ("Comment:" + reader.) Value + "<br>");
Break
Case Xmlnodetype.xmldeclaration:
Response.Write ("XmlDeclaration:" + "XML version= ' 1.0 '" + "<br>");
Break
Case XmlNodeType.Document:
Break
Case Xmlnodetype.documenttype:
Response.Write ("DocumentType:" + reader.) Name + "-----" + reader. Value + "<br>");
Break
Case XmlNodeType.EntityReference:
Response.Write ("EntityReference:" + reader.) Name + "<br>");
Break
Case XmlNodeType.EndElement:
Response.Write ("EndElement:" + reader.) Name + "<br>");
Break
}
}
}
catch (Exception ex)
{
Response.Write (ex. Message.tostring ());
}
Finally
{
if (reader!= null)
Reader. Close ();
}
}