views:

949

answers:

2

Greetings!

If I have XML such as this:

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

I'd like to get all of the Choice items in the "BetaSection", regardless of the "Set" that they belong to. I've tried the following:

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

But to no avail. How would I go about this?

Thanks.

+4  A: 

You don't need the where clause at all - you just need to change the Elements call to be Descendants:

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

(I've converted it from a query expression to simple dot notation as I don't think the query expression was really helping you.)

Jon Skeet
Shouldn't it be .Descendants("Choice")
Rohan West
Yup, it's .Descendants("Choice"). Thx to both :)
Bullines
Oops, yes. Fixed.
Jon Skeet
A: 

I'd write this instead. I prefer the SQL syntax instead of method syntax but is a matter of taste...

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 );
        }
    }
}
Eugenio Miró