如何获取XML中的特定元素

时间:2017-04-21 08:56:04

标签: c# xml xsd

<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#中查找特定的员工姓名,假设我有一个XML文件,其中有超过100个员工详细信息,现在使用微粒员工电话号码查找所有员工详细信息。怎么可能?

4 个答案:

答案 0 :(得分:0)

如果您想要使用特定电话号码的员工列表,您可以尝试:

 readinessProbe:
      httpGet:
        path: /xyzapi/health
        port: 8888
      initialDelaySeconds: 30
      periodSeconds: 30
      timeoutSeconds: 30
      successThreshold: 2
      failureThreshold: 5

以下是ID列表:

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

答案 1 :(得分:0)

创建根标签Employees作为Employee的容器。

<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}");

答案 2 :(得分: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 + "']");

答案 3 :(得分:0)

使用Linq to 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;