C# 和 LINQ - 在 xml 查询中获取父元素的名称
C# & LINQ - Get names of parent elements in xml query
我有一个 XML 文件,其中包含 IP 地址及其分配的 locations/floors,如下所示:
<Locations>
<LOCATION1>
<FLOOR1>
<SECTION1>
<IP>10.10.10.10</IP>
<IP>etc....
</SECTION1>
</FLOOR1>
</LOCATION1>
.....
我试图做的是查询 IP 地址和 return 父元素的名称。我能够查询该 IP,但我没有运气弄清楚如何获取父元素名称(即 SECTION1、FLOOR1、LOCATION1)。这是我用于查询 xml 以查找 IP 的代码,我现在只用它 returning 值来验证我的查询是否成功:
var query = from t in xmlLocation.Descendants("IP")
where t.Value.Equals(sIP)
select t.Value;
var xDoc = XDocument.Load(filename);
var ip = xDoc.XPathSelectElement("//IP['10.10.10.10']");
var names = ip.Ancestors().Select(a => a.Name.LocalName).ToList();
名称将包含 SECTION1, FLOOR1, LOCATION1, Locations
如果您事先知道这些名称,您也可以将它们用于 select 正确的节点
var ip = xDoc.XPathSelectElement("//LOCATION1/FLOOR1/SECTION1/IP['10.10.10.10']");
或
var section = xDoc.XPathSelectElement("//LOCATION1/FLOOR1/SECTION1[IP['10.10.10.10']]");
试试这个:
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 >
代码
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();
}
}
}
下面的代码获取位置、楼层和部分
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();
}
}
}
我有一个 XML 文件,其中包含 IP 地址及其分配的 locations/floors,如下所示:
<Locations>
<LOCATION1>
<FLOOR1>
<SECTION1>
<IP>10.10.10.10</IP>
<IP>etc....
</SECTION1>
</FLOOR1>
</LOCATION1>
.....
我试图做的是查询 IP 地址和 return 父元素的名称。我能够查询该 IP,但我没有运气弄清楚如何获取父元素名称(即 SECTION1、FLOOR1、LOCATION1)。这是我用于查询 xml 以查找 IP 的代码,我现在只用它 returning 值来验证我的查询是否成功:
var query = from t in xmlLocation.Descendants("IP")
where t.Value.Equals(sIP)
select t.Value;
var xDoc = XDocument.Load(filename);
var ip = xDoc.XPathSelectElement("//IP['10.10.10.10']");
var names = ip.Ancestors().Select(a => a.Name.LocalName).ToList();
名称将包含 SECTION1, FLOOR1, LOCATION1, Locations
如果您事先知道这些名称,您也可以将它们用于 select 正确的节点
var ip = xDoc.XPathSelectElement("//LOCATION1/FLOOR1/SECTION1/IP['10.10.10.10']");
或
var section = xDoc.XPathSelectElement("//LOCATION1/FLOOR1/SECTION1[IP['10.10.10.10']]");
试试这个: 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 >
代码
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();
}
}
}
下面的代码获取位置、楼层和部分
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();
}
}
}