The problem I'm running into is I want to find the best way to add the data into the XML file if the XPath returns nothing. For example, some XPaths might already exist in the XML file, so I can easily use XmlDocument.Sel ectNodes() to give me a list of the nodes that match and then I can get/set the Value on that node if it's an attribute or InnerText if it's an element, for example.
So, if someone wants to set some data into the XML and SelectNodes() returns nothing, what's the best way for me to add that node where the XPath says it should go? There may be a simple way in the .NET 2.0 framework, but I haven't been able to find it. Since SelectNodes() returns nothing, I don't have a position to insert the data into. I thought of taking off the rightmost part of the XPath and try again, repeating until I get something back and then insert each piece back out to the full XPath, but that seemed kludgy. It would be cool to just have a CreateNode(XPat h, value) or something like that. Ideas?
Here's a simple example:
Expand|Select|Wrap|Line Numbers
- <a>
- <b>
- <c>Blah</c>
- </b>
- </a>
XPath "/a/b/d" would return nothing but the user would want it to create <d> as a sibling of <c> and set/get its value.