Read and format XML data into HTML : XML Output « XML « ASP.Net






Read and format XML data into HTML

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        //string xmlFilePath = @"C:\Employees.xml";
          string xmlFilePath = MapPath("Employees.xml");
        string employeeID = "";
        try
        {
            using (XmlReader reader = XmlReader.Create(xmlFilePath))
            {
                lblResult.Text = "<b>Employees</b>";
                lblResult.Text += "<ul/>";
                string result;
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.Name == "employee")
                        {
                            employeeID = reader.GetAttribute("id");
                        }
                        if (reader.Name == "name")
                        {
                            lblResult.Text += "<li>" + "Employee - " + employeeID;
                            lblResult.Text += "<ul>";
                            lblResult.Text += "<li>ID - " + employeeID + "</li>";
                        }
                        if (reader.Name == "firstName")
                        {
                            lblResult.Text += "<li>First Name - " + reader.ReadString() + "</li>";
                        }
                        if (reader.Name == "lastName")
                        {
                            lblResult.Text += "<li>Last Name - " + reader.ReadString() + "</li>";
                        }
                        if (reader.Name == "city")
                        {
                            lblResult.Text += "<li>City - " + reader.ReadString() + "</li>";
                        }
                        if (reader.Name == "state")
                        {
                            lblResult.Text += "<li>State - " + reader.ReadString() + "</li>";
                        }
                        if (reader.Name == "zipCode")
                        {
                            lblResult.Text += "<li>Zipcode - " + reader.ReadElementContentAsInt().ToString() + "</li>";
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.Name == "employee")
                        {
                            //Close the open formatting tags
                            lblResult.Text += "</ul>";
                            lblResult.Text += "</li>";
                        }
                    }
                }
                lblResult.Text += "</ul>";
            }
        }
        catch (Exception ex)
        {
            lblResult.Text = "An Exception occurred: " + ex.Message;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Processing the Data in an XML File</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:label id="lblResult" runat="server" />
    </div>
    </form>
</body>
</html>

<%--
<?xml version='1.0'?>
<employees>
  <employee id="1">    
    <name>
      <firstName>Nancy</firstName>
      <lastName>Lee</lastName> 
    </name>
    <city>Seattle</city>
    <state>WA</state>
    <zipCode>98122</zipCode>   
  </employee>
  <employee id="2">    
    <name>
      <firstName>Jason</firstName>
      <lastName>Wang</lastName>
    </name>
    <city>Vancouver</city>
    <state>WA</state>
    <zipCode>98123</zipCode>   
  </employee> 
</employees>

--%>
           
       








Related examples in the same category

1.Read XML file and output to client
2.Use XmlDocument (DOM) to load xml file and output to browser
3.Load XML from string and output to browser