how we can set value for xml element using XmlDocument class how we can set value for xml element using XmlDocument class xml xml

how we can set value for xml element using XmlDocument class


If you know how to select a value, then you probably know how to update one too.

XmlDocument doc = new XmlDocument();doc.Load(...);XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);nsMgr.AddNamespace("v9", "http://fedex.com/ws/ship/v9");XmlNode severityNode = doc.SelectSingleNode("//v9:Severity", nsMgr);severityNode.innerText = "FAILURE";

The important thing to know is that the <v9:Severity> node has an inner text() node, so in the example above you can't use the Node.Value property. To do that you would do something like this instead:

XmlNode severityTextNode = doc.SelectSingleNode("//v9:Severity/text()", nsMgr);severityTextNode.Value = "FAILURE";

Note the subtle differences.


Do an XPath to the superior node using the XMLDocument class and add the 2 extra nodes on the tree.