Linq XML 查询具有特定属性值的parent的后代
Linq XML query the descendants of parent with a specific value in attribute
我需要一些关于 LINQ to XML 的帮助。我有以下 XML;
<GcctTestData>
<TestDriveRequest Record="1">
<element name="Time Zone">(GMT+05:30)</element>
<element name="Requested Dealer">Concorde</element>
<element name="Model Year">1999</element>
<element name="Make">Tata</element>
<element name="Model/Vehicle Line">Indica</element>
</TestDriveRequest>
<TestDriveRequest Record="2">
<element name="Time Zone">(GMT+05:30)</element>
<element name="Requested Dealer">Kun"</element>
<element name="Model Year">2020"</element>
<element name="Make">BMW"</element>
<element name="Model/Vehicle Line">3-series</element>
</TestDriveRequest>
<TestDriveRequest Record="3">
<element name="Time Zone">(GMT+05:30)</element>
<element name="Requested Dealer">KUN Hyundai</element>
<element name="Model Year">2001</element>
<element name="Make">Hyundai</element>
<element name="Model/Vehicle Line">Verna</element>
</TestDriveRequest>
</GcctTestData>
我试过如下:
IEnumerable<XElement> xElements =
from element in root.Elements("TestDriveRequest")
where element.Attribute("Record").Value == "1"
select element;
foreach (XElement el in xElements.Descendants().Where(p => !p.HasElements))
{
int keyInt = 0;
string keyName = el.Attribute("name").Value;
while (keyValuePairs.ContainsKey(keyName))
{
keyName = $"{el.Attribute("name").Value}_{keyInt++}";
}
keyValuePairs.Add(keyName, el.Value);
}
我必须获取具有记录值 [=24] 的 元素 的元素 name 属性值=]1
但是 linq 查询没有获取它...
在执行@anu-viswan的建议后,我遇到了以下问题
您需要使用 root.Descendants
而不是 root.Elements
。
例如,
IEnumerable<XElement> xElements = from element in root.Descendants("TestDriveRequest")
where element.Attribute("Record").Value == "1"
select element;
XContainer.Elements
Returns a filtered collection of the child elements of this element or
document, in document order. Only elements that have a matching XName
are included in the collection.
XContainer.Descendants
Returns a filtered collection of the descendant elements for this
document or element, in document order. Only elements that have a
matching XName are included in the collection Output
完整代码
IEnumerable<XElement> xElements = from element in root.Descendants("TestDriveRequest")
where element.Attribute("Record").Value == "1"
select element;
foreach (XElement el in xElements.Descendants().Where(p => !p.HasElements))
{
int keyInt = 0;
string keyName = el.Attribute("name").Value;
while (keyValuePairs.ContainsKey(keyName))
{
keyName = $"{el.Attribute("name").Value}_{keyInt++}";
}
keyValuePairs.Add(keyName, el.Value);
}
示例输出
这就是我创建字典的方式:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace AscendingSequences
{
class AscendingSequences
{
const string FILENAME = @"c:\temp\test.xml";
public static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Dictionary<int, Dictionary<string, string>> dict = doc.Descendants("TestDriveRequest")
.GroupBy(x => (int)x.Attribute("Record"), y => y.Elements("element")
.GroupBy(a => (string)a.Attribute("name"), b => (string)b)
.ToDictionary(a => a.Key, b => b.FirstOrDefault()))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
我需要一些关于 LINQ to XML 的帮助。我有以下 XML;
<GcctTestData>
<TestDriveRequest Record="1">
<element name="Time Zone">(GMT+05:30)</element>
<element name="Requested Dealer">Concorde</element>
<element name="Model Year">1999</element>
<element name="Make">Tata</element>
<element name="Model/Vehicle Line">Indica</element>
</TestDriveRequest>
<TestDriveRequest Record="2">
<element name="Time Zone">(GMT+05:30)</element>
<element name="Requested Dealer">Kun"</element>
<element name="Model Year">2020"</element>
<element name="Make">BMW"</element>
<element name="Model/Vehicle Line">3-series</element>
</TestDriveRequest>
<TestDriveRequest Record="3">
<element name="Time Zone">(GMT+05:30)</element>
<element name="Requested Dealer">KUN Hyundai</element>
<element name="Model Year">2001</element>
<element name="Make">Hyundai</element>
<element name="Model/Vehicle Line">Verna</element>
</TestDriveRequest>
</GcctTestData>
我试过如下:
IEnumerable<XElement> xElements =
from element in root.Elements("TestDriveRequest")
where element.Attribute("Record").Value == "1"
select element;
foreach (XElement el in xElements.Descendants().Where(p => !p.HasElements))
{
int keyInt = 0;
string keyName = el.Attribute("name").Value;
while (keyValuePairs.ContainsKey(keyName))
{
keyName = $"{el.Attribute("name").Value}_{keyInt++}";
}
keyValuePairs.Add(keyName, el.Value);
}
我必须获取具有记录值 [=24] 的 元素 的元素 name 属性值=]1
但是 linq 查询没有获取它...
在执行@anu-viswan的建议后,我遇到了以下问题
您需要使用 root.Descendants
而不是 root.Elements
。
例如,
IEnumerable<XElement> xElements = from element in root.Descendants("TestDriveRequest")
where element.Attribute("Record").Value == "1"
select element;
XContainer.Elements
Returns a filtered collection of the child elements of this element or document, in document order. Only elements that have a matching XName are included in the collection.
XContainer.Descendants
Returns a filtered collection of the descendant elements for this document or element, in document order. Only elements that have a matching XName are included in the collection Output
完整代码
IEnumerable<XElement> xElements = from element in root.Descendants("TestDriveRequest")
where element.Attribute("Record").Value == "1"
select element;
foreach (XElement el in xElements.Descendants().Where(p => !p.HasElements))
{
int keyInt = 0;
string keyName = el.Attribute("name").Value;
while (keyValuePairs.ContainsKey(keyName))
{
keyName = $"{el.Attribute("name").Value}_{keyInt++}";
}
keyValuePairs.Add(keyName, el.Value);
}
示例输出
这就是我创建字典的方式:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace AscendingSequences
{
class AscendingSequences
{
const string FILENAME = @"c:\temp\test.xml";
public static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Dictionary<int, Dictionary<string, string>> dict = doc.Descendants("TestDriveRequest")
.GroupBy(x => (int)x.Attribute("Record"), y => y.Elements("element")
.GroupBy(a => (string)a.Attribute("name"), b => (string)b)
.ToDictionary(a => a.Key, b => b.FirstOrDefault()))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}