asp.net LoadXml code
October 24, 2008 .Net, Xml — Tags: asp.net, aspnet loadxml, aspnet readxml — 52coding
<%@ Page Language=”C#” %>
<%@ Import Namespace=”System.Xml” %>
<script runat=”server”>
void Page_Load(object sender, EventArgs e)
{
string xmlPath = Request.PhysicalApplicationPath + @”\Application_Data\Books.xml”;
XmlDocument booksDoc = new XmlDocument();
XmlDocument empDoc = new XmlDocument();
Response.ContentType = “text/xml”;
try
{
//Load the XML from the file
booksDoc.PreserveWhitespace = true;
booksDoc.Load(xmlPath);
//Write the XML onto the browser
Response.Write(booksDoc.InnerXml);
//Load the XML from a String
empDoc.LoadXml(”<employees>” +
“<employee id=’1′>” +
“<name><firstName>Nancy</firstName>” +
“<lastName>Davolio</lastName>” +
“</name><city>Seattle</city>” +
“<state>WA</state><zipCode>98122</zipCode>” +
“</employee></employees>”);
//Save the XML data onto a file
empDoc.Save(@”C:\Data\Employees.xml”);
}
catch (XmlException xmlEx)
{
Response.Write(”XmlException: ” + xmlEx.Message);
}
catch (Exception ex)
{
Response.Write(”Exception: ” + ex.Message);
}
}
</script>
books xml:
<?xml version=’1.0′?>
<!– This file represents a fragment of a book store inventory database –>
<bookstore>
<book genre=”autobiography”>
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre=”novel”>
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre=”philosophy”>
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
