0

I have an XDocument object from which I am trying to extract all the List which specify a specific criteria.

Following is my XML

<MyQueue xmlns:ns0 = "http://myprogram">  
    <ns0:QueueReport>  
        <ns0:number>001</ns0:number>
        <ns0:id>A</ns0:id>
        <ns0:name>ABC</ns0:name>
        <ns0:hours>1</ns0:hours>
    </ns0:QueueReport>
    <ns0:QueueReport>
        <ns0:number>001</ns0:number>
        <ns0:id>B</ns0:id>
        <ns0:name>ABC</ns0:name>
        <ns0:hours>2</ns0:hours>
    </ns0:QueueReport>
    <ns0:QueueReport>
        <ns0:number>001</ns0:number>
        <ns0:id>B</ns0:id>
        <ns0:name>ABC</ns0:name>
        <ns0:hours>10</ns0:hours>
    </ns0:QueueReport>
    <ns0:QueueReport>
        <ns0:number>002</ns0:number>
        <ns0:id>A</ns0:id>
        <ns0:name>ABC</ns0:name>
        <ns0:hours>12</ns0:hours>
    </ns0:QueueReport>
    <ns0:QueueReport>
        <ns0:number>003</ns0:number>
        <ns0:id>A</ns0:id>
        <ns0:name>ABC</ns0:name>
        <ns0:hours>20</ns0:hours>
    </ns0:QueueReport>
</ns0:MyQueue>

The above XML is in an XDocument object say xdoc. I write the following to exract the nodes and its children.

XNamespace b = @"http://myprogram";
var elements =  from e xdoc.Elements(b + "MyQueue")
        where e.Element(b + "QueueReport").Element(b + "number").Value == "001"
        && e.Element(b + "QueueReport").Element(b + "id").Value == "B"
        select e.Elements(b + "QueueReport").ToList();

(Please excuse the typos if any in the code, it was not written in the editor)

The elements variable does not have any results, ideally i would have expected the below two elements in the List.

<ns0:QueueReport>
    <ns0:number>001</ns0:number>
    <ns0:id>B</ns0:id>
    <ns0:name>ABC</ns0:name>
    <ns0:hours>2</ns0:hours>
</ns0:QueueReport>

    <ns0:QueueReport>
        <ns0:number>001</ns0:number>
        <ns0:id>B</ns0:id>
        <ns0:name>ABC</ns0:name>
        <ns0:hours>10</ns0:hours>
    </ns0:QueueReport>

Please help, I am very new to Linq and have little clue about how else can I achieve this.

user2179891
  • 33
  • 1
  • 4

1 Answers1

0

Here is the answer to my question, I could get this working with some mind bending..

XNamespace b = @"http://myprogram";
var elements =  from e xdoc.Elements(b + "MyQueue").(b + "QueueReport")
    where e.Element(b + "number").Value == "001"
    && e.Element(b + "id").Value == "B"
    select e;
user2179891
  • 33
  • 1
  • 4