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.