0
private void alterNodeValue(string xmlFile, string parent, string node, string newVal)
{
    XDocument xml = XDocument.Load(this.dir + xmlFile);

    if (xml.Element(parent).Element(node).Value != null)
    {
        xml.Element(parent).Element(node).Value = newVal;
    }
    else
    {
        xml.Element(parent).Add(new XElement(node, newVal)); 
    }

    xml.Save(dir + xmlFile); 
}  

Why does this throw

System.NullReferenceException was unhandled by user code

on this line

if (xml.Element(parent).Element(node).Value != null)

?

I'm guessing it's because the XML node doesn't exist, but that's what the != null is suppose to check for. How do I fix that?

I've tried several things and they ALL throw the same exception at some point during the not null check.

Thanks for any help.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
PiZzL3
  • 2,092
  • 4
  • 22
  • 30

6 Answers6

7

Either xml.Element(parent) or the Element(node) you are trying to access from the return value of xml.Element(parent) is null.

Restructuring like this will enable to you see which one it is:

var myElement = xml.Element(parent);
if (xmyElement != null)
{
    var myNode = myElement.Element(node);
    if(myNode != null)
    {
      myNode.Value = newVal;
    }
}

Update:

From your comment it looks like you want to do this:

if (xml.Element(parent).Element(node) != null)  // <--- No .Value
{
    xml.Element(parent).Element(node).Value = newVal;
}
else
{
    xml.Element(parent).Add(new XElement(node, newVal)); 
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • parent exists, node doesn't. I'm trying to catch `Element(node) == null` and add it through the `else` statement. – PiZzL3 Mar 27 '11 at 19:15
  • 1
    @PiZzL3 - If you know it doesn't exist, why are you doing a `.Value` on the _non existent_ node? – Oded Mar 27 '11 at 19:17
  • I'm still learning how to use `XDocument` properly. I don't know all the details. – PiZzL3 Mar 27 '11 at 19:19
  • @PiZzL3 - If the node doesn't exist, it will be a `null` object. Any call on that would result in the exception. – Oded Mar 27 '11 at 19:37
  • Ahh k, thanks for the help! I thought calling `.Value` would grab the `null` so I could compare it. – PiZzL3 Mar 27 '11 at 21:22
1

It is almost certainly because this returns null:

xml.Element(parent)
dkackman
  • 15,179
  • 13
  • 69
  • 123
1

You need to check if:

Element(node) != null

Before you call .Value. If Element(node) == null, then the call to .Value will throw a null reference exception.

Dan

debracey
  • 6,517
  • 1
  • 30
  • 56
1

Try changing your if statement to this:

if (xml.Element(parent).Element(node) != null)

If the node in the parent element is null, you cannot access a member of a null object.

rgmills
  • 383
  • 1
  • 8
1

At least:

private void alterNodeValue(string xmlFile, string parent, string node, string newVal)
{
    XDocument xml = XDocument.Load(this.dir + xmlFile);
    XElement parent = xml.Element(parent).Element(node);
    if (parent  != null)
    {
        parent.Value = newVal;
    }
    else
    {
        xml.Element(parent).Add(new XElement(node, newVal)); 
    }    
    xml.Save(dir + xmlFile); 
}  

Better:

private void alterNodeValue(string xmlFile, string parent, string node, string newVal)
{
    string path = System.IO.Path.Combine(dir, xmlFile);
    XDocument xml = XDocument.Load(path );
    XElement parent = xml.Element(parent).Element(node);
    if (parent != null)
    {
        XElement node = parent.Element(parent);
        if (node != null)
        {
            node.Value = newVal;
        }
        else
        {
            // no node
        }
    }
    else
    {
        // no parent
    }    
    xml.Save(path); 
}  
abatishchev
  • 98,240
  • 88
  • 296
  • 433
0
        if (xml.Element(parent) != null)
        {
            var myNode = xml.Element(parent).Element(node);
            if (node != null)
                myNode.Value = newVal;
        }
        else
        {
            xml.Element(parent).Add(new XElement(node, newVal));
        }