views:

198

answers:

3

Below the xml doc

    <Root>
        <Global>
        </Global>
        <local>
            <section name="A">
                <subsection name="A">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
                <subsection name="B">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
            </section>
            <section name="B">
                <subsection name="A">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
                <subsection name="B">
                    <innersection name="A">
                        <Property1>
                        </Property1>
                    </innersection>
                    <innersection name="B">
                        <Property1>
                        </Property1>
                    </innersection>
                </subsection>
            </section>
        </local>
    </Root>

Now i want the property1 whose section name="B" and subsection name="B" and innersection name="B" in one single query using linq to xml.

+4  A: 

EDIT: Removed LINQ to XML "normal" style of query as ssg's is better. I wanted to leave the XPath version though. It's untested, but I think it should work...

var properties = doc.XPathSelectElements(
 "//section[@name='B']/subsection[@name='B']/innersection[@name='B']/property1");
Jon Skeet
Wouldn't this traverse complete DOM instead of starting by filtering down section's under local?
ssg
Yes, it would. I strongly suspect that for most real life cases it would work in the blink of an eye though :)
Jon Skeet
That's easily fixed by making the query start with `/Root/local/section` rather than `//section`.
Robert Rossney
@Robert: ssg's comment was probably more in reference to the query that I edited out a while ago :)
Jon Skeet
.......It was :)
ssg
+5  A: 

Here is my take, alternative to Jon's, assuming Property1 occurs only once inside an innersection and you need only that one:

var Property1 = doc.Root.Elements("local").Elements("section")
    .Where(x => x.Attribute("name") == "B").Elements("subsection")
    .Where(x => x.Attribute("name") == "B").Elements("innersection")
    .Where(x => x.Attribute("name") == "B").Element("Property1");
ssg
+1  A: 

Since this question was also tagged vb.net, here is the equivalent of ssg's query in vb.net:

Dim Property1 = doc.<local>.<section>.Where(Function(x) x.@name = "B") _
                           .<subsection>.Where(Function(x) x.@name = "B") _
                           .<innersection>.Where(Function(x) x.@name = "B") _
                           .<Property1>
Dennis Palmer