0

问候!

如果我有这样的 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”中的所有选择项,无论它们属于哪个“集合”。我尝试了以下方法:

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
                 };

但无济于事。我该怎么办?

谢谢。

4

2 回答 2

6

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

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

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

于 2008-11-27T20:38:06.980 回答
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 );
        }
    }
}
于 2008-11-28T00:44:28.120 回答