views:

53

answers:

1
+1  Q: 

Linq to Xml query

I have a xml structure like this:

<Items>
    <Configuration>
        <ConfigurationSetting>Setting1</ConfigurationSetting>
        <ConfigurationSetting>Setting2</ConfigurationSetting>
    </Configuration>
    <MetaData>
    ...
    </MetaData>
    <Group>
        <GroupType>MyType1</GroupType>
        <GroupType>MyType2</GroupType>
    </Group>
    <Group>  <--- Looking for this Element
        <Reference>MyReference1</Reference>
        <Reference>MyReference2</Reference>
    </Group>
    <Group>
        <GroupType>MyType3</GroupType>
        <GroupType>MyType4</GroupType>
    </Group>
</Items>

I want to write a Linq query that returns the first "Group" Element that contains a Sub-Element "Reference".

Thanks for your help.

+8  A: 

Something like:

        var grp = doc.Root.Elements("Group")
            .Where(g  => g.Elements("Reference").Any())
            .FirstOrDefault();

Or as xPath (perhaps in XmlDocument): @"/Items/Group[Reference]"

Marc Gravell
i did try everything but it didnt work.. coudn't believe that the solution is that easy ;-) thank you!
gsharp