ikantspelwurdz
Well-known member
- Joined
- Dec 8, 2009
- Messages
- 49
- Programming Experience
- 1-3
Take this XML element:
And this code - myXe is type "XElement":
The first two lines simulate input from an external system. The rest of it is my own code. This works just fine, and does what I want it to do.
But now the external system is sending information about Bob, also using the XPath schema:
Now my code crashes, because XPathSelectElement is returning Nothing.
I need the resulting xml to look like this:
How can I do that? I'd rather not have to make an XPath walker. It is OK to assume that the XML will never contain repeating elements (e.g. root will never have more than one Alice, and Bob will never have more than one HiredDate, new values should simply overwrite the old ones). It's also OK to assume that incoming XPath strings will always take the form of "/root/node/nodes", though the number of subnodes won't always be two (it will always be at least one).
VB.NET:
<root>
<Alice>
<HiredDate>1/1/2016</HiredDate>
</Alice>
</root>
And this code - myXe is type "XElement":
VB.NET:
dim address as string = "/root/Alice/HiredDate"
dim value as string = "2/1/2016"
myXe.XPathSelectElement(address).Value = value
The first two lines simulate input from an external system. The rest of it is my own code. This works just fine, and does what I want it to do.
But now the external system is sending information about Bob, also using the XPath schema:
VB.NET:
dim address as string = "/root/Bob/HiredDate"
dim value as string = "2/11/2016"
myXe.XPathSelectElement(address).Value = value
Now my code crashes, because XPathSelectElement is returning Nothing.
I need the resulting xml to look like this:
VB.NET:
<root>
<Alice>
<HiredDate>2/1/2016</HiredDate>
</Alice>
<Bob>
<HiredDate>2/11/2016</HiredDate>
</Bob>
</root>
How can I do that? I'd rather not have to make an XPath walker. It is OK to assume that the XML will never contain repeating elements (e.g. root will never have more than one Alice, and Bob will never have more than one HiredDate, new values should simply overwrite the old ones). It's also OK to assume that incoming XPath strings will always take the form of "/root/node/nodes", though the number of subnodes won't always be two (it will always be at least one).