Cannot set a value on node type 'Element'

#xml

Posted by admin on September 01, 2019

Dealing with XML can be a pain at the best of times, but running in to this error can be a pain… Why does it happen and what’s the fix?

Why? You cant map to the value property on Element Nodes… You may have code that looks like this…

string value = “foo”;
string value = “bar”;
var sessionNode = xmlDocument.CreateElement(name);
sessionNode.Value = value; // The problem happens here…

The Fix:

Change to InnerText

string value = “foo”;
string value = “bar”;
var sessionNode = xmlDocument.CreateElement(name);
sessionNode.InnerText = value; // All Working now :)

Hope this helps someone. Nick