简体   繁体   中英

How to get an XML attribute value with Linq to XML?

I'm trying to get just the specific employee Id out of an XML file so that I can go look up some Groups the employee is associated with and then add those Groups as an element to the XML.

First I need the employeeId so I can then go look up some info and I'm not understanding how to get it. I'm guessing I'm not getting it because of all the nested nodes.

Here is the XML structure.

<CXIXML>
  <Directives>
    <Updates>
      <Emp tasEmpID="00123" lName="Doe" fName="John" empStatus="A" city="HDQ" />
      <Emp tasEmpID="00456" lName="Smith" fName="Jane" empStatus="A" city="HDQ" />
    </Updates>
  </Directives>
</CXIXML>

Here is what I tried but I'm not getting the ID.

private static void SetGroupAssociations(string xmlFile)
        {
            XElement xelement = XElement.Load(xmlFile);
            IEnumerable<XElement> employees = xelement.Elements();
            foreach (var employee in employees)
            {
                var employeeId = from e in employee.Descendants("Emp")
                                 select new
                                 {
                                     Id = e.Element("tasEmpID")
                                 };

                Console.WriteLine(employeeId);
            }
        }

Ultimately I need to end up with something like the following. So, any suggestions on how to add the new element after the specific employee node would be great but first I have at least get the employee so I can look up their groups.

<CXIXML>
  <Directives>
    <Updates>
      <Emp tasEmpID="00123" lName="Doe" fName="John" empStatus="A" city="HDQ" />
      <Group groupId="1">
      <Group groupId="5">
      <Group groupId="12">
      <Emp tasEmpID="00456" lName="Smith" fName="Jane" empStatus="A" city="HDQ" />
      <Group groupId="1">
    </Updates>
  </Directives>
</CXIXML>

That looks similar than what you already had. Then within the foreach you can add sentences to your process.

var document = XDocument.Load(xmlFile);
var employees = document.Descendants("Emp");
foreach (var employee in employees)
{
    var employeeId = employee.Attribute("tasEmpID").Value;
    Console.WriteLine(employeeId);
}   

I've put document in a separate variable, so that you'll be able to save your changes using this object.

I guess your xml structure is not correct, what you describe the groups would be inside of each Emp, so I'm going to assume that.

To get an employee by an attribute you can do the following:

 XElement xelement = XElement.Load(xmlFile);
 var emp=xelement.Descendants("Emp")
                 .FirstOrDefault(e=>(string)e.Attribute("tasEmpID")=="00456");

Now to add the groups to the current employee, just use Add method:

emp.Add(groups.Select(g=>new XElement("Group",new XAttribute("groupId",g.Id))));

This is assuming groups variable is an IEnumerable<Group>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM