在XML文件中搜索记录 [英] Searching Records in XML File
问题描述
我要搜索从xml文件中搜索记录的文件.下面是我的xml文件.
dtable
id 2 id
名称jigar名称
phone_no 12345 phone_no
/dtable
dtable
id 3 id
姓名ajay姓名
phone_no 7777 phone_no
/dtable
dtable
id 4 id
名字bhargav名字
phone_no 4444 phone_no
/dtable
我想做的是,当用户搜索id ="2"时,它应该显示姓名和电话号码.我的问题是如何从xml文件中搜索?有什么帮助? "h2_lin">解决方案
尝试以下代码
XmlDcoument xDoc =新的XmlDocument(); xDoc.Load("YourXml.XML"); XmlNode xNode = xDoc.DocumentElement.SelectSingleNode("dtable [id = 2]");
这将为您提供id = 2
的dtable节点
现在,要读取其他节点,请使用以下
foreach(xNode中的XmlNode子级) { Response.Write(child.LocalName); Response.Write(:"); Response.Write(child.InnerText); Response.Write("\ n"); }
希望能有所帮助.如果是这样,请将答案标记为解决方案和/或投票.
谢谢
Milind
使用以下链接:
http://forums.asp.net/t/1318271.aspx/1 [ ^ ]
使用LINQ与运营商一起搜索XML记录 [ ^ ]
http://www.dreamincode.net/forums/topic/52453 -linq-to-xml-updating-a-record/ [ http://stackoverflow.com/questions/11406431/searching-in-xml-document [ ^ ]
谢谢,
Ambesha
I want to make search which searches records from xml file.Below is my xml file.
dtable
id 2 id
name jigar name
phone_no 12345 phone_no
/dtable
dtable
id 3 id
name ajay name
phone_no 7777 phone_no
/dtable
dtable
id 4 id
name bhargav name
phone_no 4444 phone_no
/dtable
i want to do is,When user searches for id="2",it should display name and phone no.My question is how to search from xml file?any help is appreciated.
Try following code
XmlDcoument xDoc = new XmlDocument(); xDoc.Load("YourXml.XML"); XmlNode xNode = xDoc.DocumentElement.SelectSingleNode("dtable[id=2]");
This will give you the dtable node which has id=2
Now, to read the other node use following
foreach (XmlNode child in xNode) { Response.Write(child.LocalName); Response.Write(":"); Response.Write(child.InnerText); Response.Write("\n"); }
Hope that helps. If it does, mark the answer as solution and/or upvote.
Thanks
Milind
Use the following links:
http://forums.asp.net/t/1318271.aspx/1[^]
Search XML records with Operators using LINQ[^]
http://www.dreamincode.net/forums/topic/52453-linq-to-xml-updating-a-record/[^]
http://stackoverflow.com/questions/11406431/searching-in-xml-document[^]
Thanks,
Ambesha
这篇关于在XML文件中搜索记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!