Question

I have following Xml

<Main>
         <Order Id="1262">
            <Product>
               <Name>Prod1</Name>
                   <Barcode>1234</Barcode>
                </Product>
                 <Product>
                   <Name>Prod1</Name>
                   <Barcode>1234</Barcode>
                </Product>
                <Product>
                     <Name>Prod2</Name>
                   <Barcode>2345</Barcode>
               </Product>

            </Order>

        <Order Id="1263">
               <Product>
               <Name>Prod1</Name>
                   <Barcode>1234</Barcode>
                </Product>
                 <Product>
                   <Name>Prod1</Name>
                   <Barcode>1234</Barcode>
                </Product>
                <Product>
                     <Name>Prod2</Name>
                   <Barcode>2345</Barcode>
               </Product>
                </Order>
</Main>


XmlDocument xml=new XmlDocument();
xml.Load(path);

Now I want to select only nodes whose barcode is 1234 from Node whose orderId=1263. My code is

 string OrderId="1262"
   string ReadedBarcode ="1234"

    XmlNode ONode = xml.SelectSingleNode("//Order[@Id='" + OrderId + "']");
     XmlNodeList BarCodeNodeList = ONode.SelectNodes("//Product/Barcode[text()='" + ReadedBarcode + "']");

But I dont know why all the nodes from document having innertext 1234 are getting selected. that means even node from <Order Id="1263"> this node is getting selected.

Any Solutions?

Was it helpful?

Solution

This should do it

        XmlDocument xml=new XmlDocument();
        xml.Load(path);
        string OrderId = "1262";
        string ReadedBarcode = "1234";

        XmlNodeList BarCodeNodeList = xml.SelectNodes("//Order[@Id='" + OrderId + "']"+"//Product/Barcode[text()='" + ReadedBarcode + "']");

Also, your XML is invalid, it is missing some start tags, it should be

<Main>
  <Order Id="1262">
    <Product>
    <Name>Prod1</Name>
    <Barcode>1234</Barcode>
    </Product>
    <Product>
      <Name>Prod1</Name>
      <Barcode>1234</Barcode>
    </Product>
    <Product>
      <Name>Prod2</Name>
      <Barcode>2345</Barcode>
    </Product>

  </Order>

  <Order Id="1263">
    <Product>
    <Name>Prod1</Name>
    <Barcode>1234</Barcode>
    </Product>
    <Product>
      <Name>Prod1</Name>
      <Barcode>1234</Barcode>
    </Product>
    <Product>
      <Name>Prod2</Name>
      <Barcode>2345</Barcode>
    </Product>
  </Order>
</Main>

OTHER TIPS

I know you wrote that you are 'stuck' with XmlDocument, but the reason you gave sounded like a decision not based on the technical constraints, rather on preference. I believe in a tool-for-the-job, so please forgive me for writing a solution you may not like, but it might persuade you of the merits in using XDocument where you see the benefit.

The XML you posted was not syntactically correct, the sample below shows the corrections where I have added the missing Product starter nodes.

Here's what I would suggest, code written in LinqPad. The Dump() method just spits the variable out to a console. Also, I did not know which 'node' you wanted to find, so I am returning the Order node in this sample.

Cheers, Aaron

var doc = XDocument.Parse(@"
<Main>
    <Order Id=""1262"">
        <Product>
            <Name>Prod1</Name>
            <Barcode>1234</Barcode>
        </Product>
        <Product>
            <Name>Prod1</Name>
            <Barcode>1234</Barcode>
        </Product>
        <Product>
            <Name>Prod2</Name>
            <Barcode>2345</Barcode>
        </Product>
    </Order>
    <Order Id=""1263"">
        <Product>
            <Name>Prod1</Name>
            <Barcode>1234</Barcode>
        </Product>
        <Product>
            <Name>Prod1</Name>
            <Barcode>1234</Barcode>
        </Product>
        <Product>
            <Name>Prod2</Name>
            <Barcode>2345</Barcode>
        </Product>
    </Order>
</Main>
");

var barcode = "1234";
var orderId = "1263";
var found = (
    from row in doc.Root.Descendants("Order")
    where 
      row.Attribute("Id") != null && 
      row.Attribute("Id").Value == orderId && 
      row.Descendants("Barcode").Any(a => a.Value == barcode)
    select row).ToList();

found.Dump();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top