c# - Linq XML 查询属性中具有特定值的父级的后代

标签 c# linq linq-to-xml

我需要有关 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);
            }                

我必须获取记录值为 1 的父元素的 元素 的元素 name 属性值

但是 linq 查询没有获取它...

实现@anu-viswan的建议后,我面临以下问题 enter image description here

最佳答案

您需要使用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.后代

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);
}  

示例输出

enter image description here

关于c# - Linq XML 查询属性中具有特定值的父级的后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59643060/

相关文章:

c# - out 和 ref 可以用作临时变量吗?

c# - 我的应用程序占用的内存随着时间的推移不断增加

c# - 枚举期间的 LINQ 转换

c# - 我可以重构我的 linq select 吗?

c# - XML.Linq-根据另一个值获取值

c# - 根据子节点对整个 xdocument 进行排序

C# 从 LinQ XDocument 加载数据表

c# - EF Core 保存所需属性的空值

c# - View 中的 Rowspan (mvc4)

c# - X文档遍历