简体   繁体   English

Linq to XML嵌套查询

[英]Linq to XML nested query

I'm having an issue getting a LINQ query to work. 我在使用LINQ查询时遇到问题。 I have this XML: 我有这个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. 我的Device对象包含一个DeviceInterfaces列表,我需要从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 嵌套的foreach循环对我来说似乎有点乱

Cheers 干杯

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. 这里的基本点是你在设备上做一种“subselect”(它是一个Descendant ),寻找它包含的所有Interface元素。

It creates a new DeviceInterface for each "interface" under each device. 它为每个设备下的每个“接口”创建一个新的DeviceInterface

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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