XmlReaderSettings with XmlReader
XmlReaderSettings.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";
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;
try
{
using (XmlReader reader = XmlReader.Create(xmlFile, settings))
{
string xmlContent;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
xmlContent = "";
if(reader.Name == "book")
{
if (reader.HasAttributes)
{
xmlContent += reader.GetAttribute("ID").ToString() + "| ";
}
}
if (reader.Name == "name")
{
xmlContent += "<b>" + reader.ReadString().ToString() + "</b><br />";
}
if (reader.Name == "author")
{
xmlContent += " <i>" + reader.ReadString().ToString() + "</i><br />";
}
if(reader.Name == "price")
{
xmlContent += " $" + reader.ReadString().ToString() + "<br /><br />";
}
Label1.Text += xmlContent;
}
}
}
}
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>XmlReaderSettings: How to use XmlReaderSettings with XmlReader in asp.net Xml</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy; font-style:italic;">XML Example: XmlReader and XmlReaderSettings</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="false"
ForeColor="OrangeRed"
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 ReadState: How to get present read state of XmlReader in asp.net Xml
- XmlReader HasAttributes and GetAttribute: How to read attribute in asp.net Xml
- WriteAttributeString: How to write attribute in xml file in asp.net
- WriteComment: How to write comment in xml file in asp.net
- XmlWriter and XmlWriterSettings: How to use XmlWriterSettings in asp.net
- XmlWriter: How to write Xml file programmatically in asp.net