LINQ to XML新手问题:按节点名称返回节点

时间:2008-11-27 20:32:51

标签: c# xml c#-3.0 linq-to-xml

问候!

如果我有这样的XML:

<Root>
    <AlphaSection>
    .
    .
    .
    </AlphaSection>

    <BetaSection>
        <Choices>
            <SetA>
                <Choice id="choice1">Choice One</Choice> 
                <Choice id="choice2">Choice Two</Choice>
            </SetA>
            <SetB>
                <Choice id="choice3">Choice Three</Choice> 
                <Choice id="choice4">Choice Four</Choice>
            </SetB>
        </Choices>
    </BetaSection>

    <GammaSection>
    .
    .
    .
    </GammaSection>
</Root>

我想获得“BetaSection”中的所有Choice项目,无论它们属于哪个“Set”。我尝试了以下内容:

var choiceList = from choices in myXDoc.Root.Element("BetaSection").Elements("Choices")
                 where (choices.Name == "Choice")
                 select new
                 {
                     Name = choices.Attribute("id").Value,
                     Data = choice.Value
                 };

但无济于事。我该怎么做呢?

感谢。

2 个答案:

答案 0 :(得分:6)

您根本不需要where子句 - 您只需要将Elements调用更改为Descendants:

var choiceList = myXDoc.Root
                       .Element("BetaSection")
                       .Descendants("Choice")
                       .Select(element => new
                               {
                                  Name = element.Attribute("id").Value,
                                  Data = element.Value;
                               });

(我已经将它从查询表达式转换为简单的点表示法,因为我认为查询表达式并没有真正帮助你。)

答案 1 :(得分:0)

我会写这个。我更喜欢SQL语法而不是方法语法,但这是一个品味问题......

class Program
{
    static void Main(string[] args)
    {
        String     xml          = @"<Root>
                                        <AlphaSection></AlphaSection>
                                        <BetaSection>
                                            <Choices>
                                                <SetA>
                                                    <Choice id='choice1'>Choice One</Choice>
                                                    <Choice id='choice2'>Choice Two</Choice>
                                                </SetA>
                                                <SetB>
                                                    <Choice id='choice3'>Choice Three</Choice>
                                                    <Choice id='choice4'>Choice Four</Choice>
                                                </SetB>
                                            </Choices>
                                        </BetaSection>
                                        <GammaSection></GammaSection>
                                    </Root>";
        XElement    xmlElement  = XElement.Parse(xml);
        var         choiceList  = from c in xmlElement.Descendants().Elements("Choice")
                                  select new {
                                      Name = c.Attribute("id").Value,
                                      Data = c.Value
                                  };
        foreach (var choice in choiceList) {
            Console.WriteLine("Name: {0} Data: {1}", choice.Name, choice.Data );
        }
    }
}