How can I create/update an XML node that may or may not exist?
- by ScottBai
Is there a method available (i.e. without me creating my own recursive method), for a given xpath (or other method of identifying the hierarchical position) to create/update an XML node, where the node would be created if it does not exist? This would need to create the parent node if it does not exist as well. I do have an XSD which includes all possible nodes.
i.e.
Before:
<employee>
<name>John Smith</name>
</employee>
Would like to call something like this:
CoolXmlUpdateMethod("/employee/address/city", "Los Angeles");
After:
<employee>
<name>John Smith</name>
<address>
<city>Los Angeles</city>
</address>
</employee>
Or even a method to create a node, given an xpath, wherein it will recursively create the parent node(s) if they do not exist?
As far as the application (if it matters), this is taking an existing XML doc that contains only populated nodes, and adding data to it from another system. The new data may or may not already have values populated in the source XML.
Surely this is not an uncommon scenario?