Results 1 to 2 of 2

Thread: [2005] How to construct a valid "xpath"?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2005
    Posts
    30

    [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:
    1. Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
    2.         'construct the new node
    3.         Dim newNode As xmlFile.XmlNode = _
    4.                 xmlCOA.CreateNode(Xml.XmlNodeType.Element, "",  txtElementName.Text, "")
    5.         newNode.InnerText() = txtItemName.Text
    6.  
    7.         'construct the xpath
    8.         Dim txt As String = TreeView1.SelectedNode.Text
    9.         Dim fullPath As String = TreeView1.SelectedNode.FullPath 'return value is: #document/.......
    10.         fullPath = fullPath.substring(9) 'cut "#document"
    11.  
    12.         'this is valid, but it cannot locate the exact node
    13.         'Dim xn As xmlFile.XmlNode = xmlCOA.SelectSingleNode("//" + txt)
    14.  
    15.         'error: xpath is not valid
    16.         Dim xn As xmlFile.XmlNode = xmlCOA.SelectSingleNode(fullpath)
    17.  
    18.         xn.AppendChild(newNode)
    19.     End Sub

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width