繁体   English   中英

找不到使用LINQ-to-XML选择XML信息的正确语法时遇到问题

[英]Having problems to find the right syntax to select XML info using LINQ-to-XML

我一直在使用: doc.Descendants("ipAddress")从XML文件获取所有IP地址,但是现在我试图仅获取每个网络适配器的IP地址。 这是XML文件部分:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<server xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="ServerInfo.xsd" id="57672acc-4ba7-4876-811a-1629eca853ed">
  <networkAdapters>
    <networkAdapter id="6ad45274-6077-4a46-9b5c-d4e7be712310" name="NVIDIA nForce 10/100/1000 Mbps Networking Controller">
      <ipAddresses>
        <ipAddress address="192.168.1.1" subnetMask="255.255.255.252" index="0" />
      </ipAddresses>
    </networkAdapter>
    <networkAdapter id="eb872ba4-695e-451a-9505-90b6b0539833" name="TEAM : Command and Control">
      <ipAddresses>
        <ipAddress address="76.229.35.32" subnetMask="255.255.255.128" index="0" />
        <ipAddress address="76.229.35.31" subnetMask="255.255.255.128" index="1" />
        <ipAddress address="76.229.35.5" subnetMask="255.255.255.128" index="2" />
      </ipAddresses>
    </networkAdapter>
  </networkAdapters>
</server>
var networkAdapters = doc.Descendants("networkAdapter")
                         .Select(n => new NetworkAdapter
{
    networkAdapterId                = new Guid((string)n.Attribute("id")),
    serverId                        = server.serverId,
    name                            = (string)n.Attribute("name")
}).ToList();

List<IpAddress> ipAddressList = new List<IpAddress>();
foreach (var networkAdapter in networkAdapters)
{
    IpAddress item = new IpAddress();
    //get the network adapter id
    item.networkAdapterId = networkAdapter.networkAdapterId;
    //need the code to loop through all ip addresses for this particular network adapter????
    //{
        //item.address    = "attribute "address"    
        //item.subnetMask = "attribute subnetMask"
        //item.index      = "attribute index"  
        ipAddressList.Add(item);
    //}
}

有任何想法吗? 谢谢

您可以使用以下查询来获取IpAdress列表,而不必使用foreach手动遍历NetworkAdapter列表:

List<IpAddress> ipAddressList = new List<IpAddress>();
var query = doc.Descendants("networkAdapter")
               .Descendants("ipAddress")
               .Select(n => new IpAddress
                              {
                                  address = n.Attribute("address").Value,
                                  subnetMask = n.Attribute("subnetMask").Value,
                                  index = n.Attribute("index").Value
                              });
ipAddressList.AddRange(query);
// get networkAdapter element first
var networkElement = doc.Root
                        .Element("networkAdapters")
                        .Elements("networkAdapter")
                        .First(a => (string)a.Attribute("id") == networkAdapter.networkAdapterId.ToString());

// iterate over ipAddress elements
foreach(var address in networkElement.Element("ipAddresses").Elements("ipAddress"))
{
    // you have to declare item here,
    // otherwise you'll add the same item more then once to the list
    var item = new IpAddress();
    item.networkAdapterId = networkAdapter.networkAdapterId;

    item.address    = (string)address.Attribute("address");
    item.subnetMask = (string)address.Attribute("subnetMask");
    item.index      = (int)address.Attribute("index");

    ipAddressList.Add(item);
}

您可以在foreach循环中使用以下代码来获取属于特定networkAdapter的所有IP地址:

var ipAddresses = (from ip in networkAdapter.Element("ipAddresses").Elements("ipAddress")
                     select new IpAddress {
                                            Address = ip.Attribute("address").Value,
                                            subnetMask = ip.Attributes("subnetMask").Value,
                                            index = ip.Attributes("subnetMask").Value
                                           }).ToList();

如果我正确地理解了您想将ipAddresses存储在单独的数组中,那么如果您可以将Ip Adress集合添加到networkAdapter类中,那么我猜会更好,那么您可以这样做:

networkAdapter.IpAddressCollection.AddRange(ipAdresses);

暂无
暂无

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

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