Linq to XML nested query

纵饮孤独 提交于 2019-12-10 05:45:06

问题


I'm having an issue getting a LINQ query to work. I have this XML:

<devices> 
   <device id ="2142" name="data-switch-01">
     <interface id ="2148" description ="Po1"/>
   </device>
   <device id ="2302" name="data-switch-02">
     <interface id ="2354" description ="Po1"/>
     <interface id ="2348" description ="Gi0/44" />
   </device>
 </devices>

And this code:

var devices = from device in myXML.Descendants("device")
              select new
              {
                  ID = device.Attribute("id").Value,
                  Name = device.Attribute("name").Value,
               };

foreach (var device in devices)
{
    Device d = new Device(Convert.ToInt32(device.ID), device.Name);

    var vIfs = from vIf in myXML.Descendants("device")
                  where Convert.ToInt32(vIf.Attribute("id").Value) == d.Id
                  select new
                  {
                      ID = vIf.Element("interface").Attribute("id").Value,
                      Description = vIf.Element("interface").Attribute("description").Value,
                  };
    foreach (var vIf in vIfs)
    {
        DeviceInterface di = new DeviceInterface(Convert.ToInt32(vIf.ID), vIf.Description);
        d.Interfaces.Add(di);
    }

    lsDevices.Add(d);
}

My Device object contains a List of DeviceInterfaces which I need to populate from the XML. At the moment my code only populates the first interface, any subsequent ones are ignored and I can't understand why.

I'd also appreciate any comments on whether or not this is the right way to do it. The nested foreach loops seem a bit messy to me

Cheers


回答1:


IEnumerable<Device> devices = 
  from device in myXML.Descendants("device")
  select new Device(device.Attribute("id").Value, device.Attribute("name").Value)
  {
     Interfaces = (from interface in device.Elements("Interface")
                   select new DeviceInterface(
                        interface.Attribute("id").Value,
                        interface.Attribute("description").Value)
                  ).ToList() //or Array as you prefer
  }

The basic point here is that you do a sort of "subselect" on device (which is a Descendant), seeking all the Interface elements that it contains.

It creates a new DeviceInterface for each "interface" under each device.




回答2:


Quick and dirty

var query = from device in document.Descendants("device")
            select new
            {
                ID = device.Attribute("id").Value,
                Name = device.Attribute("name").Value,
                Interfaces = from deviceInterface in device.Descendants("interface")
                             select new
                             {
                                 ID = deviceInterface.Attribute("id").Value,
                                 Description = deviceInterface.Attribute("description")
                             }
            };


来源:https://stackoverflow.com/questions/2495384/linq-to-xml-nested-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!