1

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?

2
  • 1
    Your example XML is invalid. There's at least a closing quotation mark missing on 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. Commented Jan 18, 2014 at 20:01
  • Ok, it seems the 2 should be a quotation mark (German keyboard). I edited the post and removed all unused namespaces as well. Commented Jan 18, 2014 at 20:08

1 Answer 1

1

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()
          };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.