的xml:
<PrimaryLink>
<LinkID>p1</LinkID>
<SecondaryLink>
<LinkID>s1</LinkID>
<LeftMenu>
<NavLinks>
<LinkID>n1</LinkID>
</NavLinks>
<NavLinks>
<LinkID>n2 </LinkID>
</NavLinks>
</LeftMenu>
</SecondaryLink>
</PrimaryLink>
C#:
var menuItems1 = from c in xMenuElement.Elements("GlobalNavigation").Elements("PrimaryLink")
select c;
通过给出n1作为输入,我喜欢p1-&gt; s1-&gt; n1
答案 0 :(得分:1)
你的问题很不清楚,但我想你想要的东西是:
var query = xml.Descendants("LinkID")
.Where(x => (string) x == "n1")
.Single()
.AncestorsAndSelf("LinkID")
.Reverse()
.Select(x => (string) x);
这将返回“p1”,“s1”和“n1”的序列。