DevJoe
DevJoe

Reputation: 419

Reading XML values from different namespaces in same LINQ Query

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?

Upvotes: 1

Views: 440

Answers (1)

har07
har07

Reputation: 89295

Notice that extra in in your linq-to-xml code is <Extras> element, and since <Speed> and <Rpm> are not direct child of <Extras> you can't select any of them by using extra.Element(ns3 + "elementName"). You can use Descendants instead of Element in this case :

XNamespace ns =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.Descendants(ns3 + "Speed").FirstOrDefault() ??
                                                     0.0,
                                                 Rpm =
                                                     (int?)
                                                     extra.Descendants(ns3 + "Rpm").FirstOrDefault() ?? 0
                                             }).ToList()
                         }).ToList()
          };

Upvotes: 1

Related Questions