1
私はこのようなXMLを持っている:XMLにLINQのは、1対多の要素名を取得し、属性に名前/値
<manager firstName="Dat" lastName="Bossman">
<employee firstName="Jonathan" lastName="Smith" preferredName="Jon" />
<employee christianName="Jane" lastName="Doe" />
<employee lastName="Jones" firstInitial="A" middleName="J" />
</manager>
私は、すべての要素名のコレクション/リストを返したいのですが属性名が{"firstName"、 "preferredName"、 "christianName"、 "firstInitial"、 "middleName"}にある場合の属性名/属性値コンビネーション
上記のXMLを指定すると、リストは次のようになります。
elementName attributeName attributeValue
============ ============== ===============
manager firstName Dat
employee firstName Jonathan
employee preferredName Jon
employee christianName Jane
employee firstInitial A
employee middleName J
私は正しい要素を返すいくつかのLINQを持っていますが、それを上のプロパティを得るのに役立つコレクション/リストに変換する方法がわかりません。
List<string> desiredAttributes = new List<string>();
desiredAttributes.AddRange(new string[] { "firstName", "preferredName", "christianName", "firstInitial", "middleName" });
XDocument document = XDocument.Load(xmlStream);
IEnumerable<XElement> theResults = document.Descendants()
.Where(el => el.Attributes().Any(att => desiredAttributes.Contains(att.Name.LocalName)));
「親」を使ってツリーを見直す...私はあまりにも忙しすぎて、それについて逆に考えていませんでした。ありがとう。 – ThunderFrame