[2005] How to construct a valid "xpath"?
I have a DOM tree (xmlCOA).
It is associated with a TreeView (TreeView1).
I would like to add a new node to the DOM tree.
After I construct the new node, I locate a particular node for adding the new node as its child.
I use "fullPath" to construct the xpath to locate the targeted node.
But, I don't understand something: the fullPath return: #document/.......
Should I trim the "#document" away?
No matter I cut it or not, it causes error: wrong xpath expression.
Why?
visual basic code:--------------------------------------------------------------------------------
VB Code:
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
'construct the new node
Dim newNode As xmlFile.XmlNode = _
xmlCOA.CreateNode(Xml.XmlNodeType.Element, "", txtElementName.Text, "")
newNode.InnerText() = txtItemName.Text
'construct the xpath
Dim txt As String = TreeView1.SelectedNode.Text
Dim fullPath As String = TreeView1.SelectedNode.FullPath 'return value is: #document/.......
fullPath = fullPath.substring(9) 'cut "#document"
'this is valid, but it cannot locate the exact node
'Dim xn As xmlFile.XmlNode = xmlCOA.SelectSingleNode("//" + txt)
'error: xpath is not valid
Dim xn As xmlFile.XmlNode = xmlCOA.SelectSingleNode(fullpath)
xn.AppendChild(newNode)
End Sub
Re: [2005] How to construct a valid "xpath"?
It would be easire to load the XMLNode assoicated with each TreeNode into the Tag property of the TreeNode. Then you can just add the new node as a child directly to the selected node. Otherwise give a full example of what the SelectedNode.FullPath is returning. I'd imagine that it would take was tweeking considering it is not meant to work directly with xml like that.