0

子が具体的なattribute.valueを持つXelementattribute.valueを見つけたい。

string fatherName =  xmlNX.Descendants("Assembly")
                           .Where(child => child.Descendants("Component")
                               .Where(name => name.Attribute("name").Value==item))
                           .Select(el => (string)el.Attribute("name").Value); 

attribute.valueを取得するにはどうすればよいですか?それはブール値だとはどういう意味ですか?

もともと編集済み 私は次のXMLを持っています:

<Assembly name="1">
  <Assembly name="44" />
  <Assembly name="3">
     <Component name="2" />
  </Assembly>
  </Assembly>

その子(XElement)がexpecific attribute.valueを持つattribute.valueを取得する必要があります。この例では、attribute.value == "2"の子の親を検索しているため、文字列"3"を取得します。

4

2 に答える 2

2

ネストされたWhere句の書き方のためです。

内側の句は次のとおりです。

child.Descendants("Component").Where(name => name.Attribute("name").Value==item)

この式の結果は typeIEnumerable<XElement>であるため、外側の句は次のようになります。

.Where(child => /* an IEnumerable<XElement> */)

ただしWhere、タイプの引数が必要でFunc<XElement, bool>あり、ここで -- を渡すことになるFunc<XElement, IEnumerable<XElement>>ため、エラーが発生します。

指定されたコードからは意図がまったく明確でないため、修正版は提供していません。それに応じて質問を更新してください。

アップデート:

次のようなものが必要なようです。

xmlNX.Descendants("Assembly")
     // filter assemblies down to those that have a matching component
     .Where(asm => asm.Children("Component")
                     .Any(c => c.name.Attribute("name").Value==item))
     // select each matching assembly's name
     .Select(asm => (string)asm.Attribute("name").Value)
     // and get the first result, or null if the search was unsuccessful
     .FirstOrDefault();
于 2012-06-14T12:14:48.307 に答える
1

私はあなたが欲しいと思います

string fatherName =  xmlNX.Descendants("Assembly")
                           .Where(child => child.Elements("Component").Any(c => (string)c.Attribute("name") == item))
                           .Select(el => (string)el.Attribute("name")).FirstOrDefault();
于 2012-06-14T12:31:55.927 に答える