ASP.Net/XML/XML Output
Содержание
Load XML from string and output to browser
<source lang="csharp">
<%@ 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>
</source>
Read and format XML data into HTML
<source lang="csharp">
<%@ 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 = "Employees"; 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 += "
- ";
lblResult.Text += "
- ID - " + employeeID + " ";
} if (reader.Name == "firstName") {lblResult.Text += "
} if (reader.Name == "lastName") {lblResult.Text += "
} if (reader.Name == "city") {lblResult.Text += "
} if (reader.Name == "state") {lblResult.Text += "
} if (reader.Name == "zipCode") {lblResult.Text += "
} } else if (reader.NodeType == XmlNodeType.EndElement) { if (reader.Name == "employee") { //Close the open formatting tagslblResult.Text += ""; lblResult.Text += "";
} } } 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">
<asp:label id="lblResult" runat="server" />
</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> --%>
</source>
Read XML file and output to client
<source lang="csharp">
<%@ 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>
--%>
</source>
Use XmlDocument (DOM) to load xml file and output to browser
<source lang="csharp">
<%@ 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> --%>
</source>