0

I have a file with the following content (myfile.xml). I have to get all content coming under (including product node) a product with id=1.

<products>  
  <product id="1">
       <category>q</category>  
  </product>    
  <product id="2">      
       <category>w</category>    
  </product>   
  <product id="3">       
  <category>e</category>   
 </product>
</products>`

i.e. the result should be :

 <product id="1"> 
      <category>q</category>
  </product> 

How can I do this?

Nikana Reklawyks
  • 3,233
  • 3
  • 33
  • 49
Kuttan Sujith
  • 7,889
  • 18
  • 64
  • 95
  • follow-up [using Xmltextreader](http://stackoverflow.com/questions/10680553/getting-data-from-xml-by-attribute-value-c-sharp-using-xmltextreader) – Nikana Reklawyks Oct 05 '12 at 08:10

2 Answers2

1

using XPath in Linq

var root = XElement.Load("myfile.xml");  
root.XPathSelectElements( "/products/product[@id=1]");
Tilak
  • 30,108
  • 19
  • 83
  • 131
0
var root = XElement.Load("path to the file");
var node = root.Descendants("product").FirstOrDefault(e=>e.Attribute("id").Value == "1");
RePierre
  • 9,358
  • 2
  • 20
  • 37