Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a node from xPath

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
GB
Hey all again.
Is it possible to create a node/element in an xml doc that from an xPath.

What I am doing is checking to see if it exists.
If it doesnt I want to create it from the xPath query.

Code:
saveNode("//Appearance//Top", xd).InnerText = main.Top

Function saveNode(ByVal xPath As String, ByVal xd As Xml.XmlDocument, Optional ByVal defaultSetting As Object = "") As Xml.XmlNode
        If Not xd.SelectSingleNode(xPath) Is Nothing Then
            'send the node back
            saveNode = xd.SelectSingleNode(xPath)
        Else
            'create the node
            'send the node back
        End If
    End Function

}...the bane of my life!
 
I am thinking i need to splitt the path and then loop through it checking and building each node.
Can anyone help me on that?

}...the bane of my life!
 
never mind sorted it
Code:
saveNode("//Appearance//Note//Font",xd)
 
Function saveNode(ByVal xPath As String, ByVal xd As Xml.XmlDocument) As Xml.XmlNode
 If xd.SelectSingleNode(xPath) Is Nothing Then
  nodeFromPath(xPath, xd)
 End If
 Return (xd.SelectSingleNode(xPath))
End Function
Sub nodeFromPath(ByVal xPath As String, ByVal xd As Xml.XmlDocument)
 If xd.SelectSingleNode(xPath) Is Nothing Then
  Dim root As Xml.XmlNodeList
  Dim newNode As Xml.XmlNode
  Dim parentPath As String = Nothing
  Dim splitPath() As String
  Dim node As String
  Dim currNode As Xml.XmlNode
  Dim currentPath As String
  splitPath = Split(xPath, "//")
  For Each node In splitPath
   If node <> "" Then
    currentPath = parentPath
    parentPath &= "//" & node
    If xd.SelectSingleNode(parentPath) Is Nothing Then
     MsgBox(parentPath & " does not exist")
     root = xd.SelectNodes(currentPath)
     newNode = xd.CreateNode(Xml.XmlNodeType.Element, node, "")
     currNode = root(0).InsertAfter(newNode, root(0).ChildNodes(0))
    End If
   End If
  Next
  'clean up
  root = Nothing : newNode = Nothing : parentPath = Nothing : splitPath = Nothing : node = Nothing : currNode = Nothing : currentPath = Nothing
 End If
End Sub

}...the bane of my life!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top