<%--
Code Revised from
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback)
by Thiru Thangarathinam
# Publisher: Wrox (January 18, 2006)
# Language: English
# ISBN: 0764596772
--%>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlExpressions.Items.Add("//book/title");
ddlExpressions.Items.Add("//book[@genre='novel']/title");
ddlExpressions.Items.Add("//book/author/first-name");
ddlExpressions.Items.Add("//book[@genre='philosophy']/title");
ddlExpressions.Items.Add("//book/price");
ddlExpressions.Items.Add("//book[3]/title");
ddlExpressions.SelectedIndex = 0;
UpdateDisplay();
}
}
void ddlExpressions_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateDisplay();
}
void UpdateDisplay()
{
lstOutput.Items.Clear();
string xmlPath = MapPath("MyBooks.xml");
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
XmlNodeList nodeList = doc.DocumentElement.SelectNodes(ddlExpressions.SelectedItem.Text);
foreach (XmlNode child in nodeList)
{
lstOutput.Items.Add("Node Name:" + child.Name);
lstOutput.Items.Add("Node Value:" + child.FirstChild.Value);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>XPath Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Select the XPath Expression:
<asp:DropDownList ID="ddlExpressions" AutoPostBack="true" runat="server" Width="410px" OnSelectedIndexChanged="ddlExpressions_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<asp:ListBox ID="lstOutput" runat="server" Width="587px" Height="168px"></asp:ListBox>
</div>
</form>
</body>
</html>
<%--
<?xml version='1.0'?>
<bookstore>
<book genre="autobiography">
<title>The Autobiography</title>
<author>
<first-name>A</first-name>
<last-name>B</last-name>
</author>
<price>999</price>
</book>
<book genre="novel">
<title>The Man</title>
<author>
<first-name>C</first-name>
<last-name>D</last-name>
</author>
<price>8888</price>
</book>
</bookstore>
--%>
|