Saturday, July 13, 2013

How to read and process Xml file element data in asp.net

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>
Default.aspx
-----------------
<%@ Page Language="C#" %>  
<%@ Import Namespace="System.Xml" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<script runat="server">  
    protected void Page_Load(object sender, System.EventArgs e)  
    {
        string xmlFile = Request.PhysicalApplicationPath + @"ITBookStore.xml";   //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 == "name")  
                        {  
                            xmlContent += "Book: " + reader.ReadString().ToString() + "<br />";  
                        }  
                        if (reader.Name == "author")  
                        {  
                            xmlContent += "Author: " + reader.ReadString().ToString() + "<br />";  
                        }  
                        if(reader.Name == "price")  
                        {  
                            xmlContent += "Price: $" + reader.ReadString().ToString() + "<br />";  
                        }  
                        if(reader.Name == "type")  
                        {  
                            xmlContent += "Type: " + reader.ReadString().ToString() + "<br />";  
                        }  
                        if(reader.Name == "image")  
                        {  
                            xmlContent += "Image: " + 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>XmlReader: How to read and process Xml file element data in asp.net</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:Navy; font-style:italic;">XML Example: Reading Xml Element Data</h2>  
        <asp:Label   
             ID="Label1"  
             runat="server"  
             Font-Bold="false"  
             ForeColor="Crimson"  
             Font-Size="Large"  
             Font-Names="Comic Sans MS"  
             >  
        </asp:Label>  
    </div>  
    </form>  
</body>  
</html> 

0 comments:

Post a Comment