简体   繁体   English

C#和LINQ-在xml查询中获取父元素的名称

[英]C# & LINQ - Get names of parent elements in xml query

I have an XML file with IP addresses and their assigned locations/floors that looks like this: 我有一个带有IP地址及其分配的位置/楼层的XML文件,如下所示:

<Locations>
   <LOCATION1>
      <FLOOR1>
         <SECTION1>
            <IP>10.10.10.10</IP>
            <IP>etc....
         </SECTION1>
      </FLOOR1>
   </LOCATION1>
.....

What I am attempting to do is get query for an IP address and return the names of the parent elements. 我正在尝试做的是获取IP地址查询并返回父元素的名称。 I am able to query for that IP but I have not had any luck figuring out how to get the parent elements names (ie SECTION1, FLOOR1, LOCATION1). 我可以查询该IP,但是我还没有弄清楚如何获取父元素名称(即SECTION1,FLOOR1,LOCATION1)。 Here is the code I am using for querying xml to find the IP, I just have it returning the value at the moment to verify my query was successful: 这是我用于查询xml来查找IP的代码,我只是让它返回当前值以验证查询是否成功:

var query = from t in xmlLocation.Descendants("IP")
            where t.Value.Equals(sIP)
            select t.Value;

Try this : XML 试试这个:XML

<?xml version="1.0" encoding="utf-8" ?>
<Locations>
  <LOCATION1>
    <FLOOR1>
      <SECTION1>
        <IP>10.10.10.10</IP>
        <IP>20.20.20.20</IP>
      </SECTION1>
    </FLOOR1>
    <FLOOR2>
      <SECTION1>
        <IP>30.30.30.30</IP>
        <IP>40.40.40.40</IP>
      </SECTION1>
    </FLOOR2>
  </LOCATION1>
</Locations >

​

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("LOCATION1").Elements().Select(x => new
            {
                parent = x.Name.ToString(),
                ip = x.Descendants("IP").Select(y => (string)y).ToList()
            }).ToList();

        }
    }

}
​

The code below gets location, floor, and section 下面的代码获取位置,楼层和部分

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("Locations").Elements().Select(x => x.Elements().Select(y => y.Elements().Select(z => new {
                location = x.Name.ToString(),
                floor = y.Name.ToString(),
                section = z.Name.ToString(),
                ip = z.Descendants("IP").Select(i => (string)i).ToList()
            })).SelectMany(a => a)).SelectMany(b => b).ToList();

        }
    }

}
​
var xDoc = XDocument.Load(filename);
var ip = xDoc.XPathSelectElement("//IP['10.10.10.10']");
var names = ip.Ancestors().Select(a => a.Name.LocalName).ToList();

names will contain SECTION1, FLOOR1, LOCATION1, Locations 名称将包含SECTION1, FLOOR1, LOCATION1, Locations

If you know those name beforehand you can also use them to select the correct node 如果您事先知道这些名称,也可以使用它们来选择正确的节点

var ip = xDoc.XPathSelectElement("//LOCATION1/FLOOR1/SECTION1/IP['10.10.10.10']");

or 要么

var section = xDoc.XPathSelectElement("//LOCATION1/FLOOR1/SECTION1[IP['10.10.10.10']]");

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

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