如何通过使用给定的关键字获取xml数据 [英] how can i get the xml data by using given keyword
问题描述
大家好,
如何使用搜索等关键字获取xml数据。
1 - >我有1个带数据的xml文件。(one.xml)
2 - >上传了one.xml文件
3 - >我有一个文本框和按钮。
4 - >我给了文本框中的一个关键词ex-2
现在如何获取XML文件的相关数据
我的XML文件
Hi all,
How can i get the xml data by using keyword like search.
1--> i have 1 xml file with data.(one.xml)
2--> uploaded the one.xml file
3--> i have one textbox and button.
4--> i am given to one key word in textbox ex-2
now how to get the related data for XML file
my XML file
<?xml version="1.0" encoding="ISO-8859-1"?>
<company>
<employee id="109">
<fname>Nohn</fname>
<lname>Graph</lname>
<age>28</age>
</employee>
<employee id="110">
<fname>Anna</fname>
<lname>Laoe</lname>
<age>32</age>
</employee>
<employee id="111">
<fname>Nllan</fname>
<lname>Woods</lname>
<age>25</age>
</employee>
<employee id="113">
<fname>Greg</fname>
<lname>Muray</lname>
<age>24</age>
</employee>
<employee id="114">
<fname>Fannie</fname>
<lname>Now</lname>
<age>36</age>
</employee>
<employee id="115">
<fname>Hello</fname>
<lname>World</lname>
<age>11</age>
</employee>
<employee id="116">
<fname>What</fname>
<lname>How</lname>
<age>11</age>
</employee>
</company>
1)我的搜索关键字是'2'
2)我的搜索关键字是'N'
然后显示与我的关键字相关的XML数据
1喜欢o / p
28
32 br / >
25
2喜欢o / p
Nohn
Nllan
现在
请帮帮我
感谢和问候
1) my search keyword is '2'
2) My Search Keyword is 'N'
then show the XML data related to my keyword
1 like o/p
28
32
25
2 like o/p
Nohn
Nllan
Now
please help me
thanks and regards
推荐答案
首先需要定义查询中使用的字段。
您可以将文档加载到XmlDocument
的一个实例中。剪切可能有所帮助:
You first need to define which fields to use in your query.
You may load the document into one instance ofXmlDocument
. This snipped could help:
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load( "Put here your file name" );
XmlNodeList Nodes = XmlDoc.SelectNodes( "put here your XPath query/filter" );
foreach (XmlNode Node in Nodes)
{
/// And do something usefull for you with the results... if any ;-)
}
这里有XPath指南: http: //www.w3schools.com/xpath/ [ ^ ]。
试试这个代码
Try this code
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(x);
XmlNodeList Nodes = XmlDoc.SelectNodes("//text()[contains(.,'N')]");
foreach (XmlNode Node in Nodes)
{
Console.WriteLine(Node.InnerText);
}
这篇关于如何通过使用给定的关键字获取xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!