use XPath, "//Author[name='Jane']/price", for exampleauthors.xml:
<Authors>
<Author>
<name>Mark</name>
<price>2200</price>
</Author>
<Author>
<name>Jane</name>
<price>3500</price>
</Author>
<Author>
<name>Tom</name>
<price>4000</price>
</Author>
</Authors>testxml.aspx:
<%@ Import Namespace="System.Xml" %>
<script language="C#" runat="Server">
void Page_Load(Object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("authors.xml"));
XmlElement el = (XmlElement)xmldoc.SelectSingleNode("//Author[name='Jane']/price");
if (el != null)
Response.Write("Jane's price:" + el.InnerText);
else
Response.Write("Jane Not found"); el = (XmlElement)xmldoc.SelectSingleNode("//Author[name='Jack']/price");
if (el != null)
Response.Write("Jack's price:" + el.InnerText);
else
Response.Write("Jack Not found");
}
</script>
<Authors>
<Author>
<name>Mark</name>
<price>2200</price>
</Author>
<Author>
<name>Jane</name>
<price>3500</price>
</Author>
<Author>
<name>Tom</name>
<price>4000</price>
</Author>
</Authors>testxml.aspx:
<%@ Import Namespace="System.Xml" %>
<script language="C#" runat="Server">
void Page_Load(Object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("authors.xml"));
XmlElement el = (XmlElement)xmldoc.SelectSingleNode("//Author[name='Jane']/price");
if (el != null)
Response.Write("Jane's price:" + el.InnerText);
else
Response.Write("Jane Not found"); el = (XmlElement)xmldoc.SelectSingleNode("//Author[name='Jack']/price");
if (el != null)
Response.Write("Jack's price:" + el.InnerText);
else
Response.Write("Jack Not found");
}
</script>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货