Mega Code Archive

 
Categories / ASP.Net / XML
 

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