使用C#在xml文件中搜索特定值 [英] Searching for a certain value in an xml file using C#

查看:134
本文介绍了使用C#在xml文件中搜索特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨对所有人



我一直在寻找这个问题,似乎没有找到一个链接,这将有助于mw也许它是搜索我的关键词我打字错了,那么这就是我的问题。



我有一个xml文件,其内容如下:



Hi To all

I''ve been searching for this problem and seem not find a link that will help mw maybe its the key words for searching that i am typing wrong, well then here is my problem.

I have an xml file the has the following contents as below :

<?xml version="1.0" encoding="utf-8" ?> 
- <Employees>
- <Employee>
  <ID>1</ID> 
  <FirstName>David</FirstName> 
  <LastName>Smith</LastName> 
  <Salary>10000</Salary> 
  </Employee>
- <Employee>
  <ID>3</ID> 
  <FirstName>Mark</FirstName> 
  <LastName>Drinkwater</LastName> 
  <Salary>30000</Salary> 
  </Employee>
- <Employee>
  <ID>4</ID> 
  <FirstName>Norah</FirstName> 
  <LastName>Miller</LastName> 
  <Salary>20000</Salary> 
  </Employee>
- <Employee>
  <ID>12</ID> 
  <FirstName>Cecil</FirstName> 
  <LastName>Walker</LastName> 
  <Salary>120000</Salary> 
  </Employee>
  </Employees>





写入xml文件我没有问题。

我想要检索的不是文件的全部内容,而只检索文件的全部内容员工按一个属性搜索,说出员工属性。如果你在教程中帮助我解决我使用C#的问题,我将永远感激不尽。



提前谢谢



Writing to an xml file i have no problem.
What I would like to do retrieve not the whole contents of the file, but retrieve only the one the employees searching by one attribute, say the employee attribute. If you were to assist me in tutorials that would help me over come my problem using C# i would be forever grateful.

Thanks in advance

推荐答案

嗯,你有几种可能性:

1)将其反序列化为ac#集合。 使用序列化加载对象并将对象保存到XML [<] a href =http://www.codeproject.com/Articles/4491/Load-and-save-objects-to-XML-using-serializationtarget =_ blanktitle =New Window> ^ ],使用泛型的XML序列化 [ ^ ]

2)使用XQuesy / XPath库: .NET中的XSL 2.0和XQuery 1.0 [ ^ ]

3)使用LINQ to XML: http: //msdn.microsoft.com/en-us/library/bb387098.aspx [ ^ ], http://code.msdn.microsoft.com/windowsdesktop/LINQ-LINQ-to-XML-using-6f2d2218 [ ^ ]

4)您甚至可以在某些有限的情况下使用正则表达式,例如您指定的内容。
Well, you have several possibilities:
1) Deserialize this to a c# collection. Load and save objects to XML using serialization[^], XML serialization using Generics[^]
2) Use XQuesy/XPath libraries: XSL 2.0 and XQuery 1.0 in .NET[^]
3) Use LINQ to XML: http://msdn.microsoft.com/en-us/library/bb387098.aspx[^], http://code.msdn.microsoft.com/windowsdesktop/LINQ-LINQ-to-XML-using-6f2d2218[^]
4) You can even use regular expressions in some limited situations, like what you specified.


XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(@"XMLFile1.xml"));
            XmlNodeList addlst = doc.SelectNodes("Employees/Employee/FirstName/firstName");


您可以使用 XPath 来实现此目的。如果您不熟悉 XPath 查询,请查看 w3schools XPath教程 [ ^ ]。您还可以找到世界上最小的XML XPath教程 [< a href =http://mydotnet.wordpress.com/2008/05/29/worlds-smallest-xml-xpath-tutorial/target =_ blanktitle =New Window> ^ ]有趣。



以下愚蠢的例子搜索员工''Norah''并报告她的详细信息(警告:没有错误检查):

You might use XPath for the purpose. If you are not familiar with XPath queries, then have a look at w3schools XPath tutorial[^]. You may also find "World’s Smallest XML XPath Tutorial"[^] interesting.

The following silly example searches for the Employee ''Norah'' and reports her details (warning: no error checking):
using System;
using System.Xml;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("empl.xml"); // Your file
            
            XmlNode xnd = doc.DocumentElement.SelectSingleNode("./Employee[FirstName/text()='Norah']");
            Console.WriteLine("ID = {0}, LastName = {1}, Salary = {2}", xnd.ChildNodes[0].InnerText, xnd.ChildNodes[2].InnerText, xnd.ChildNodes[3].InnerText);
        }
    }
}


这篇关于使用C#在xml文件中搜索特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆