user3645184
user3645184

Reputation: 25

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:

<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. I am able to query for that IP but I have not had any luck figuring out how to get the parent elements names (i.e. 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:

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

Upvotes: 1

Views: 889

Answers (2)

Eser
Eser

Reputation: 12546

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

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']]");

Upvotes: 0

jdweng
jdweng

Reputation: 34421

Try this : 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();

        }
    }

}
​

Upvotes: 1

Related Questions