0

I am trying to put a username and password child into an XML element. I want it so that when the user enters their information, it will check to see if the elements exist. If it does, it will check to see if it is different. if it is different, it will update. If it doesn't exist it will create a new child.

XML:

<ServersList>
    <Server>
        <ServiceName>Test Service</ServiceName>
        <Url requestQuery="default">http://localhost</Url>
        <DefaultLayers filter="off"></DefaultLayers>
    </Server>
</ServersList>

As there are many Servers, I want to search for the server with service name "Test Service", and then perform the check.

Here is the VB code that creates the DOMDOCUMENT.

Dim xmlDocServers As DOMDocument
Dim XMLFile As String

XMLFile = projectpath & "\system\WMS_Servers.xml"
If Not (ehfexist(XMLFile)) Then Exit Sub

Set xmlDocServers = New DOMDocument
xmlDocServers.async = False
xmlDocServers.resolveExternals = False
xmlDocServers.validateOnParse = False

xmlDocServers.load XMLFile

Any help would be greatly appreciated!

Thanks so much!!!

gberg927
  • 1,636
  • 9
  • 38
  • 51

1 Answers1

2

The simplest way to do this is to take advantage of XPATH to pull out your element. The XPATH selector /ServersList/Server[ServiceName/text()='myServiceName' will pull out all Server elements which have a ServiceName child element whose text matches your service name. Assuming that only one such element exists, selectSingleNode() will pick out the first which matches the selector.

If you get a node, then you should then do your test for "difference", whatever that is. If it is different, then you should update it in whatever way is necessary. If no node was found, then it is created, and I assume you need to update that as well.

Remember to call Save() on the xmlDocServers object when you have finished!

Private Sub CheckServer(ByRef xmlDocServers As MSXML2.DOMDocument, ByRef serviceName As String)

    Dim xmlService          As MSXML2.IXMLDOMElement
    Dim updateOtherValues  As Boolean

    Set xmlService = xmlDocServers.selectSingleNode("/ServersList/Server[ServiceName/text()='" & serviceName & "']")

    ' If nothing was returned, then we must create a new node.
    If xmlService Is Nothing Then
        Set xmlService = xmlDocServers.createElement("Server")
        xmlService.Text = serviceName
        xmlDocServers.documentElement.appendChild xmlService
        updateOtherValues = True
    Else
        updateOtherValues = IsDifferent(xmlService)
    End If

    If updateOtherValues Then
        DoUpdateOtherValues xmlService
    End If

End Sub

Private Function IsDifferent(ByRef xmlService As MSXML2.IXMLDOMElement) As Boolean
    IsDifferent = True
End Function

Private Sub DoUpdateOtherValues(ByRef xmlService As MSXML2.IXMLDOMElement)
    '
End Sub
Mark Bertenshaw
  • 5,594
  • 2
  • 27
  • 40