Cannot set a value on node type 'Element'

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

No Comments, be the first to leave one!

Leave a Comment

Yay! You've decided to leave a comment. That's fantastic! Please keep in mind that comments are moderated and rel="nofollow" is in use. So, please do not use a spammy keyword or a domain as your name, or it will be deleted. Let's have a personal and meaningful conversation instead. Thanks for dropping by!