XmlReader ReadState to get present read state of XmlReader
XmlReadReadState.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
string xmlFile = Request.PhysicalApplicationPath + @"App_Data\ITBookStore.xml";
try
{
using (XmlReader reader = XmlReader.Create(xmlFile))
{
string xmlContent;
Label1.Text += "Reader State: " + reader.ReadState.ToString()+ "<br /><br />";
Label1.Text += "Reading element.... " + "<br />";
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
xmlContent = "";
if (reader.Name == "name")
{
xmlContent += reader.ReadString().ToString();
xmlContent += " [Reader State: " + reader.ReadState.ToString() + "]<br />";
}
Label1.Text += xmlContent;
reader.MoveToElement();
}
}
Label1.Text += "<br />Reader State: " + reader.ReadState.ToString();
}
}
catch (Exception ex)
{
Label1.Text = "An Error Occured: " + ex.Message;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>XmlReader ReadState: How to get present read state of XmlReader in asp.net Xml</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy; font-style:italic;">XML Example: XmlReader ReadState()</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="false"
ForeColor="SeaGreen"
Font-Size="Large"
Font-Names="Comic Sans MS"
>
</asp:Label>
</div>
</form>
</body>
</html>
ITBookStore.xml
<?xml version="1.0" encoding="utf-8" ?>
<!-- This is a sample XML file for various XML example-->
<books>
<book ID="1">
<name>Adobe Flex 3: Training from the Source</name>
<author>Jeff Tapper</author>
<price>47.99</price>
<type>Flex</type>
<image>AdobeFlex3.jpeg</image>
</book>
<book ID="2">
<name>Styling Web Pages with CSS</name>
<author>Tom Negrino</author>
<price>15.99</price>
<type>CSS</type>
<image>CSS.jpeg</image>
</book>
<book ID="3">
<name>Adobe Flash CS4 Professional</name>
<author>Mark Schaeffer</author>
<price>19.99</price>
<type>Flash</type>
<image>FlashCS4.jpeg</image>
</book>
</books>

- A Sample Xml File source code for various asp.net Xml example
- XmlReader: How to read and process Xml file element name in asp.net
- XmlReader: How to read and process Xml file element data in asp.net
- XmlReader HasAttributes and GetAttribute: How to read attribute in asp.net Xml
- XmlReaderSettings: How to use XmlReaderSettings with XmlReader in asp.net Xml
- XmlWriter and XmlWriterSettings: How to use XmlWriterSettings in asp.net
- How to use WriteStartElement, WriteEndElement and WriteElementString method in asp.net Xml