views:

604

answers:

5

OK, bit of a random question, but the best way to do this is to just add the code, you'll be able to see what I mean straight away:

XML:

<?xml version="1.0" encoding="utf-8" ?>
<customers>
  <customer>
    <id>1</id>
    <name>Blah-face</name>
    <Type>1</Type>
  </customer>
  <customer>
    <id>2</id>
    <name>Blah-face-2</name>
    <Type>2</Type>
  </customer>
  <customer>
    <id>3</id>
    <name>Blah-face-3</name>
    <Type>1</Type>
    <SuperType>1</SuperType>
  </customer>
</customers>

C#:

XDocument linquee = XDocument.Load(path);

var superType = (from c in linquee.Descendants("customer")
                 where (c.Element("SuperType").Value == "1")
                 select c).ToList();

This comes up with a null error - would I need to add the "SuperType" element to each customer before it with a null value, or is there a workaround that would mean I don't have to do that?

Cheers!

+3  A: 

You should be able to just add a check for null

where c.Element("SuperType") != null 
&& [your other criteria]
Anthony Pegram
+2  A: 

Have you tried checking if the SuperType element exists before trying to read the value from it?

...
where (c.Element("SuperType") != null && c.Element("SuperType").Value == "1")
...
Andy Shellam
+5  A: 

Try this:

var superType = (from c in from c in linquee.Descendants("customer")
                 where (string) c.Element("SuperType") == "1"
                 select c).ToList();

Basically if you cast a null XElement reference to string, you'll get a null reference (which you can compare with "1").

An alternative would be to cast to int? which (IIRC) will return a null int? value if the element is missing, but go bang if it's present but non-numeric:

var superType = (from c in from c in linquee.Descendants("customer")
                 where (int?) c.Element("SuperType") == 1
                 select c).ToList();
Jon Skeet
Perfect, makes things much more simple than checking for nulls. Will "tick" in a moment.
David Archer
A: 

I would do it like this:

var superType = linquee.Descendants("customer").
    Where(c => c.Element("SuperType") != null 
        && c.Element("SuperType").Value == "1");
Dan Diplo
A: 

Should also be able to clean this kinda stuff up with extensions, something like..

public string Element_valStr(XElement xElm, string xName)
{
    if (xElm.Element(xName) == null) return string.empty;
    return xElm.Element(xName).Value;
}

and then just:

var superType = (from c in linquee.Descendants("customer")  
                  where (c.Element_valStr("SuperType") == "1")
                  select c).ToList();
Simon Karlsson