LINQ/XML:如果属性名包含某个关键字,则获取属性值

LINQ/XML: Get the attribut value if the attribut name contains a certain key word

由于我对 LINQ 和 xDocument 不是很熟悉,所以我很难实现以下内容: 我有一个 XML 文件,看起来像

<document>
   <attribut1/>
   <attribut2>
      <house price="100" location="UK" id-ext="Id-100"/>
      <house price="300" location="GB" id-int="Id-101"/>
   </attribut2>
   <attribut3/>
</document>

用伪代码说话我需要类似

的东西

输入:xDocument

输出:包含具有所有值的字符串的列表,即本例中的“Id-100”,以及属性名称中包含“id-ext”的那些属性。因此,我尝试获取那些名称中包含某些特定字母的属性的值。

我已经搜索过与此处所述类似的建议: How to search entire XML file for keyword? 但重点是,这里返回了整个 XML-Node,但我无法将其分解为属性名称。

对于如何在应用“后代”后移动一个以获取属性名称中包含某些关键字的那些属性的值的任何建议,我将不胜感激。

使用字典

            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string, List<XElement>> dict = doc.Descendants("house")
                .Where(x => x.Attribute("id-ext") != null)
                .GroupBy(x => (string)x.Attribute("id-ext"))
                .ToDictionary(x => x.Key, y => y.ToList());

假设“keywords are contained in the attribut name”是指作为字符串的属性名包含特定的子字符串,并且该属性可能出现在文档中的任何元素上:

var doc = XDocument.Parse(@"<document>
<attribut1/>
<attribut2>
   <house price='100' location='UK' id-ext='Id-100'/>
   <house price='300' location='GB' id-int='Id-101'/>
</attribut2>
<attribut3/>
</document>");

foreach (var s in doc
  .Descendants()
  .SelectMany(e => e.Attributes())
  .Where(a => a.Name.LocalName.Contains("id-ext"))
  .Select(a => a.Value))
{
    Console.WriteLine(s);
}