LINQ to XML新手问题:返回节点值和正负值
问题描述:
我有一些这样的XML:
<Root>
<AlphaSection>
.
.
.
</AlphaSection>
<BetaSection>
<Choices>
<SetA>
<Choice id="choice1">
<Title>Choice 1 Title</Title>
<Body>Choice 1 Body</Body>
</Choice>
<Choice id="choice2">
<Title>Choice 2 Title</Title>
<Body>Choice 2 Body</Body>
</Choice>
</SetA>
<SetB>
<Choice id="choice3">
<Title>Choice 3 Title</Title>
<Body>Choice 3 Body</Body>
</Choice>
<Choice id="choice4">
<Title>Choice 4 Title</Title>
<Body>Choice 4 Body</Body>
</Choice>
</SetB>
</Choices>
</BetaSection>
<GammaSection>
.
.
.
</GammaSection>
</Root>
我目前在做以下检索每个选择的ID:
var choiceList = myXDoc.Root
.Element("BetaSection")
.Descendants("Choice")
.Select(element => new
{
ID = element.Attribute("id").Value,
// Title = ?
// Body = ?
});
我也想获得值每个选择的标题和正文子节点。我会怎么做呢?谢谢。
答
element => new {
ID = element.Attribute("id").Value,
Title = element.Element("Title").Value,
Body = element.Element("Body").Value
});
答
另外的XElement提供了一系列的类型转换重载的,所以你不喜欢的东西......
element => new {
ID = (string)element.Attribute("id"),
title = (string)element.Element("Title"),
Body = (string)element.Element("Body")
});
现在我觉得愚蠢的:)谢谢。 – Bullines 2008-12-04 01:13:19