对xml 模糊查询 代码

来源:互联网 发布:软件项目管理类型 编辑:程序博客网 时间:2024/06/06 20:02

XML文件:
<Persons>
<Person id="1">
<Name>李一</Name>
<Sex>男</Sex>
</Person>
<Person id="2">
<Name>李二</Name>
<Sex>女</Sex>
</Person>
<Person id="3">
<Name>李三</Name>
<Sex>男</Sex>
</Person>
<Person id="4">
<Name>陈四</Name>
<Sex>男</Sex>
</Person>
<Person id="5">
<Name>李四</Name>
<Sex>女</Sex>
</Person>
</Persons>

现在,我希望针对“姓名”做一个模糊查询,在用户输入:“李”的时候,将姓李的资料全部显示出来

解决方法一:System.Xml.XmlDocument x = new System.Xml.XmlDocument();
x.Load(Server.MapPath("x1.xml"));
System.Xml.XmlNodeList nodes = x.SelectNodes("//Name");
foreach(System.Xml.XmlNode y in nodes)
{
if(y.InnerText.IndexOf("李") >-1)
Response.Write(y.InnerText);
}
解决方法2:System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.Load("file:///d:/test.xml");

foreach (System.Xml.XmlNode n in doc.DocumentElement.SelectNodes("Person[contains(Name,'李')]"))
{
 MessageBox.Show(n.InnerXml);
}

 
原创粉丝点击