Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
i have an xml file.

XML
<?xml version="1.0" encoding="utf-8" ?>
<Vehicle>
   <feature id = "1" name = "car">
     <propery id = "1" name = "Model Number"/>
     <property id = "2" name = "Color of car"/>
   </feature>
   <feature id = "2" name = "Bike">
      <property id = "1" name = "Model of bike"/>
      <property id ="2" name = "Compnay name"/>
   </feature>
</vehicle>


i want to search name of property element where feature name = car.

SQL
var Parameteroffeature = from xp in xelement.Elements("property")
                                   select xp.Attributes("name").Where()



please anyone help me.

thanks
Posted

Hi
Try this code..


I have used lamda expression instead of comprehension queries.

C#
XDocument xdoc = XDocument.Load("xmlfile.xml");

           var lv1ss = xdoc.Descendants("Vehicle").Descendants("feature").Select(k => new
           {
               propertyid = k.Descendants("propery").Select(a => new
                   {
                       propertyid = a.Attribute("id").Value,
                       propertyname = a.Attribute("name").Value
                   }).ToList(),
               id = k.Attribute("id").Value,
               name = k.Attribute("name").Value
           }).Where(k => k.name == "car").ToList();
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 
v3
Comments
Rajeev Jayaram 4-Dec-13 7:16am    
None of the above links refer to any 'LINQ to XML' queries.
I suggest you to refer: http://en.wikipedia.org/wiki/Language_Integrated_Query
abbaspirmoradi 4-Dec-13 7:22am    
thanks,you right.
Rajeev Jayaram 4-Dec-13 8:04am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900