I hope you guys will be able to help me out?
I have this XML structure:
<DataBase
xsi:schemaLocation="http://somestuff.new/xml http://somestuff.xsd"
xmlns:ns3="http://somestuff.new/ns3"
xmlns:ns2="http://somestuff.new/ns2"
xmlns="http://somestuff.new/ns"
xmlns:xsi="http://somestuff.new/XMLScema-instance"
xmlns:ns4="http://somestuff.new/ns4">
<Cars>
<SmallCars attribute="Something">
<Id>licenceplate</Id>
<Parts attribute="All Parts">
<Extras>
<Gauges xmlns="http://somestuff.new/ns3">
<Speed>100</Speed>
<Rpm>3200</Rpm>
</Gauges>
</Extras>
</Parts>
</SmallCars>
</Cars>
</DataBase>
I have then created a class of Cars like this:
public class Cars
{
public string CarType { get; set; }
public List<Parts> CarParts { get; set; }
}
And a Class of Parts:
public class Parts
{
public List<Gauges> CarExtras { get; set; }
}
And last but not least a class of Gauges:
public class Gauges
{
public double Speed { get; set; }
public int Rpm { get; set; }
}
Now I would like to create a LINQ query that gets the values from the Gauges part of the XML but I seem to fail in my attempt:
XDocument xml = XDocument.Load(file);
XNamespace xmlns =XNamespace.Get("http://somestuff.new/ns");
XNamespace ns3 = XNamespace.Get("http://somestuff.new/ns3");
var Cars = from car in xml.Descendants(ns + "Cars")
select new Cars
{
CarParts = (from part in car.Descendants(ns + "Parts")
select new Parts
{
CarExtras = (from extra in part.Descendants(ns + "Extras")
select new Gauges
{
Speed = (double?)extra.Element(ns3 + "Speed") ?? 0.0,
Rpm = (int?)extra.Element(ns3 + "Rpm") ?? 0
})
})
});
I have tried a lot of combinations with the namespaces because it changes when I get to Gauges but I do not get any values returned.
Hope someone can help me out here?
xmlns="http://somestuff.new/ns32. Your example XML actually uses only 2 namespaces, the default namespace on DataBase (xmlns="somestuff.new/ns") and the default namespace on Gauges (xmlns="somestuff.new/ns32"). You didn't declar the latter one in your code.