c# - Why does this throw a NullReferenceException? -


  Private Wide Alternode Valle (string xmlfile, string mangent, string node, string newwall) {XDocument xml = XDocument. Load (This.dir + xmlFile); If (xml.Element (parent). Element (node) value! = Null) {xml.Element (original). Element (node) .value = newVal; } And {xml.Element (parent). Add (New XElement (Node, Naval)); } Xml.Save (dir + xmlFile);   

Why does it throw

System.NullReferenceException user code

did not stand on this line

  if (xml.Element (parent). Element (node) value! = Null)   

?

I think this is because XML node is not present, but it is that ! = Zero to check for how I can fix this?

I have tried many things and they throw the same exception at any point during any blank investigation

Thanks for any help

You are trying to access the return value of xml.Element (parent) null .

It is necessary to reconstruct this way that you are able to see which one 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 seems that you want to:

  Xml element (parent). Element (node)! = Null) // & lt; --- No. Value {xml.Element (parent). Element (node). Value = newwell; } And {xml.Element (parent). Add (New XElement (Node, Naval)); }    

Comments