2017-04-21 161 views
0
<Employee Id="01"> 
<Name> ABC </Name> 
<Telephone> 123456789</Telephone> 
<Age> 25</Age> 
<MartialStatus> False </MartialStatus> 
</Employee> 

<Employee Id="02"> 
<Name> XYZ </Name> 
<Telephone> 00000000</Telephone> 
<Age> 25</Age> 
<MartialStatus> False </MartialStatus> 
</Employee> 

使用的电话号码是如何在C#中找到特定员工姓名,假设我有谁拥有100多个员工详细信息现在使用的颗粒员工的电话号码找到所有员工详细一个XML文件。怎么可能?如何获得特定元素在XML

+0

你尝试过什么和你在哪里卡住了? –

+0

我在xml文件中有多个员工的详细信息,现在我想用他的\她的电话号码在c#中的任何特定的员工详细信息 –

+0

我已经更新我的答案应该现在工作 –

回答

0

如果要使用特定的电话号码列表ofemployes你可以试试这个:

XElement xmlDoc = XElement.Parse(xml); 
var employee = xmlDoc.Descendants("Employee").Where(x => x.Element("Telephone")?.Value == "123456789").ToList(); 

这里是id的列表:

List<string> employeeIDs = res.Select(x => x.Attribute("Id").Value).ToList(); 
0

为员工容器创建一个根标签的员工。

<Employees> 
 
    <Employee Id="01"> 
 
    <Name> ABC </Name> 
 
    <Telephone>123456789</Telephone> 
 
    <Age> 25</Age> 
 
    <MartialStatus> False </MartialStatus> 
 
    </Employee> 
 
    <Employee Id="02"> 
 
    <Name> XYZ </Name> 
 
    <Telephone>00000000</Telephone> 
 
    <Age> 25</Age> 
 
    <MartialStatus> False </MartialStatus> 
 
    </Employee> 
 
</Employees>

 // load file XDocument 
     XDocument _doc = XDocument.Load("C:\\t\\My File2.txt"); 
     /* 
     1. Select Employees 
      2. Select the Employee Element 
       3.Search int this Employee for elements with name "Telephone" 
        4.Extract the value and compare it to your given number 
      5. Continue to the next Employee to comaire 
     6.Select the first on of all the elements that for filled the search term 
     */  

     var employee = _doc.Element("Employees") 
      .Elements("Employee") 
      .Where(x => x.Element("Telephone")? 
      .Value == "00000000") 
      .FirstOrDefault(); 

     // Get values from elements of Employee 
     string name = employee.Element("Name").Value; 
     string age = employee.Element("Age").Value; 

     MessageBox.Show($"Name: {name}, Age {age}"); 
+0

我希望所有员工的详细信息,不仅为您的代码命名我必须使用“如果”多于4或5次? –

+0

您可以像这样获得用户的元素,但这不是最佳做法。我已经更新了我的问题,所以可以在一个oneliner :) –

+0

@Vishal Prajapati它工作? –

0

方法使用XPath

xml.xml内容

<Employees> 
    <Employee Id="01"> 
     <Name> ABC </Name> 
     <Telephone> 123456789</Telephone> 
     <Age> 25</Age> 
     <MartialStatus> False </MartialStatus> 
    </Employee> 

    <Employee Id="02"> 
     <Name> XYZ </Name> 
     <Telephone> 00000000</Telephone> 
     <Age> 25</Age> 
     <MartialStatus> False </MartialStatus> 
    </Employee> 
</Employees> 

代码通过它来访问员工的电话号码

string telNo = " 123456789"; 
XmlDocument doc = new XmlDocument(); 
doc.Load(@"c:\temp\xml.xml"); 
var employee = doc.SelectSingleNode("Employees/Employee[Telephone='"+ telNo + "']"); 
0

使用LINQ到XML:

XElement xelement = XElement.Load(@"..\XMLfile1.xml"); 
      IEnumerable<XElement> employees = xelement.Elements(); 

      var elementYouNeed = employees.Where(x => x.Element("Telephone").Value.Trim() == "00000000"); 
      var nameYouNeed = elementYouNeed.ToList()[0].Element("Name").Value; 
相关问题