I'm begginer in linq to xml. i Have an xml file, i want to read it, and select object(bicycle) by Id. My test xml file is:
<Bikes>
<!--- - - - - - - - - - - - - - - - - - - -A new Bike- - - - - - - - - - - - - - - - - - - -->
<Bike Id="1">
<Big_Picture>Image</Big_Picture>
<Small_Picture>Image</Small_Picture>
<Emblem_Picture>Image</Emblem_Picture>
<Firm>Image</Firm>
<Model>Image</Model>
<Price>Image</Price>
<Colour>Image</Colour>
<Frame_Size>Image</Frame_Size>
<Description>Image</Description>
<Gears>Image</Gears>
<Groupset>Image</Groupset>
<Brakes>Image</Brakes>
<Frame_Material>Image</Frame_Material>
<Wheel>Image</Wheel>
</Bike>
</Bikes>
I want to select this bike by id (1), then put the elements of this bike in object of my class (Bike). How can i do that? My code, of course, doesn't perform the task:
XDocument xdoc = XDocument.Load("Bikes.xml");
xdoc.Descendants("Bike").Select(p => new {
id = p.Attribute("Id").Value,
picture = p.Element("Small_Picture").Value,
model = p.Element("Model").Value,
price = p.Element("Price").Value
}).ToList().ForEach(p => {
Bike bike = new Bike(p.id, p.picture, p.model, p.price);//Constructor
bikes_xml.Add(bike);
});