ASP.Net/XML/XML Output

Материал из .Net Framework эксперт
Версия от 11:51, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Load XML from string and output to browser

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        XmlDocument empDoc = new XmlDocument();
        Response.ContentType = "text/xml";
        try
        {
            //Load the XML from a String
            empDoc.LoadXml("<employees>" +
                        "<employee id="1">" +    
                        "<name><firstName>First Name</firstName>" + 
                        "<lastName>Last Name</lastName>" +                       
                        "</name><city>City</city>" +
                        "<state>WA</state><zipCode>99999</zipCode>" +
            "</employee></employees>");    
            //Save the XML data onto a file                    
            empDoc.Save(MapPath("EmployeesNew.xml"));
            Response.Write(empDoc.InnerXml);
        }
        catch (XmlException xmlEx)
        {
            Response.Write("XmlException: " + xmlEx.Message);
        }
        catch (Exception ex)
        {
            Response.Write("Exception: " + ex.Message);
        }        
    }   
</script>



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>
--%>



Read XML file and output to client

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Schema" %>
<script runat="server">    
    void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        string xmlPath = MapPath("Authors.xml");            
        XmlReader reader = XmlReader.Create(xmlPath);
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        XmlSchemaInference schema = new XmlSchemaInference();
        schemaSet = schema.InferSchema(reader);
        foreach (XmlSchema schemaObj in schemaSet.Schemas())
        {
            schemaObj.Write(Response.Output);
        }      
    }
  
</script>

<%--
<?xml version="1.0"?>
<authors>
  <author>
    <authorID>000-00-0001</authorID>
    <lastName>Nancy</lastName>
    <firstName>Lee</firstName>
    <phone>999 999-9999</phone>
    <address>9999 York St.</address>
    <city>Regina</city>
    <state>LA</state>
    <zip>99999</zip>
    <contract>true</contract>
  </author>
  <author>
    <authorID>000-00-0002</authorID>
    <lastName>First</lastName>
    <firstName>Last</firstName>
    <phone>415 986-7020</phone>
    <address>No Name St.</address>
    <city>Vancouver</city>
    <state>BC</state>
    <zip>88888</zip>
    <contract>true</contract>
  </author> 
</authors>

--%>



Use XmlDocument (DOM) to load xml file and output to browser

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        string xmlPath = MapPath("Authors.xml");
        XmlDocument booksDoc = new XmlDocument();
        Response.ContentType = "text/xml";
        try
        {
            booksDoc.PreserveWhitespace = true;
            booksDoc.Load(xmlPath);
            //Write the XML onto the browser
            Response.Write(booksDoc.InnerXml);
        }
        catch (XmlException xmlEx)
        {
            Response.Write("XmlException: " + xmlEx.Message);
        }
        catch (Exception ex)
        {
            Response.Write("Exception: " + ex.Message);
        }        
    }   
</script>

<%--
<?xml version="1.0"?>
<authors>
  <author>
    <authorID>000-00-0001</authorID>
    <lastName>Nancy</lastName>
    <firstName>Lee</firstName>
    <phone>999 999-9999</phone>
    <address>9999 York St.</address>
    <city>Regina</city>
    <state>LA</state>
    <zip>99999</zip>
    <contract>true</contract>
  </author>
  <author>
    <authorID>000-00-0002</authorID>
    <lastName>First</lastName>
    <firstName>Last</firstName>
    <phone>415 986-7020</phone>
    <address>No Name St.</address>
    <city>Vancouver</city>
    <state>BC</state>
    <zip>88888</zip>
    <contract>true</contract>
  </author> 
</authors>
--%>