文件夹6
text.xml:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table>
<EmployeeID>1</EmployeeID>
<EName>明*日</EName>
<ESex>男</ESex>
<EAge>25</EAge>
<EPlace>吉林省长春市</EPlace>
<EMoney>5000</EMoney>
</Table>
<Table>
<EmployeeID>2</EmployeeID>
<EName>张*三</EName>
<ESex>男</ESex>
<EAge>28</EAge>
<EPlace>吉林省长春市</EPlace>
<EMoney>3000</EMoney>
</Table>
<Table>
<EmployeeID>3</EmployeeID>
<EName>李*四</EName>
<ESex>女</ESex>
<EAge>23</EAge>
<EPlace>山西省长治市</EPlace>
<EMoney>3000</EMoney>
</Table>
<Table>
<EmployeeID>4</EmployeeID>
<EName>王*五</EName>
<ESex>女</ESex>
<EAge>26</EAge>
<EPlace>山西省长治市</EPlace>
<EMoney>2000</EMoney>
</Table>
</NewDataSet>
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("test.xml"));加载xml
XmlNodeList nodes;
XmlElement root = doc.DocumentElement;
nodes = root.SelectNodes("descendant::Table[EName='" + TextBox1.Text.Trim() + "']");//descendant::双冒号//选择与XPath匹配的节点集合//获取此指定的节点集合下面进行遍历
//遍历指定节点集合,遍历输出
foreach (XmlNode node in nodes)
{
if (Label1.Text == "")
{
for (int i = 0; i <= node.ChildNodes.Count - 1; i++)
{
Label1.Text = Label1.Text + node.ChildNodes[i].InnerText + "<br>";
}
}
else
{
Label1.Text = "";
for (int i = 0; i <= node.ChildNodes.Count - 1; i++)
{
Label1.Text = Label1.Text + node.ChildNodes[i].InnerText + "<br>";
}
}
}
}
html:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_6_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<table align="center" border="1" cellpadding="0" cellspacing="0" style="font-size: 9pt;
text-align: center">
<tr>
<td style="font-size: 9pt; color: #ffffff; background-color: #ff9933">
检索XML节点</td>
</tr>
<tr>
<td style="font-size: 9pt; vertical-align: middle; background-color: #ffffcc; text-align: left">
<table align="center">
<tr>
<td>
<table>
<tr>
<td style="height: 8px">
员工 ID:</td>
</tr>
<tr>
<td style="height: 8px">
员工姓名:</td>
</tr>
<tr>
<td style="height: 8px">
员工性别:</td>
</tr>
<tr>
<td style="height: 8px">
员工年龄:</td>
</tr>
<tr>
<td style="height: 8px">
员工籍贯:</td>
</tr>
<tr>
<td style="height: 8px">
员工薪水:</td>
</tr>
</table>
</td>
<td>
<asp:Label ID="Label1" runat="server" Font-Size="10pt" Width="77px"></asp:Label></td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="font-size: 9pt; background-color: #ffffcc">
请输入员工姓名:<asp:TextBox ID="TextBox1" runat="server" Width="73px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="检索节点" /></td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>