簡體   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