First, we'll start by building an XML document, as follows:
<?xml version="1.0" encoding="UTF-8"?>
<peoplelist>
<person>
<name>Tom Stafford</name>
<title>CFO</title>
</person>
<person>
<name>Jane Goodwill</name>
<title>CEO</title>
</person>
<person>
<name>Tim Daly</name>
<title>CTO</title>
<title2>CTO0</title2>
</person>
<person>
<name>John Graver</name>
<title>CSO</title>
</person>
</peoplelist>
Save it as: People.xml,:)
(1) Find the specified node in the XML document
For example, to find the <title2> of a user named ' Tim Daly ', the specific procedure is as follows.
----
<%@page language="C#"%>
<%@import namespace="System.Xml"%>
<%@import namespace="System.Xml.Xsl"%>
<%@import namespace="System.Xml.XPath"%>
<script language="c#" runat="server">
void page_load(Object obj,EventArgs e)
{
string xmlfile="people.xml",xpath;
xpath=Server.MapPath(xmlfile);
XmlDocument myDoc = new XmlDocument(); //定义一个XmlDocument对象。
myDoc.Load(xpath);
message.Text=myDoc.SelectSingleNode ("//person[name='Tim Daly']").ChildNodes.Item(2).InnerText;}
</script>
<asp:label id="message" forecolor="red" runat="server"/>
Explain:
where//Represents the child nodes of any layer inside. This will soon find what you want.
selectSingleNode is to find a single node, selectnodes can find many nodes