views:

288

answers:

1

Hi

I am trying to update a child node of an XML file IE Changing the value.

The XML file looks like this:

<user>
  <firstname>Andre</firstname>
  <lastname>Bruton</lastname>
</user>

Here is my Classic asp code:

users_firstname = "Tristan"  ''# New code to put in the XML file

Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xmlObj.async = False
xmlObj.setProperty "ServerHTTPRequest", True
xmlObj.Load(cURL)
If xmlObj.parseError.errorCode <> 0 Then
  Response.Write "Error Reading File - " & xmlObj.parseError.reason & "<p>"
End If

Set xmlList = xmlObj.getElementsByTagName("user")
For Each xmlItem In xmlList
  For Each xmlItem2 In xmlItem.childNodes
    a = xmlItem2.nodeName
    if a = "firstname" then firstname = xmlItem2.text
    if a = "lastname" then lastname = xmlItem2.text
  Next
Next

If firstname <> users_firstname Then
  Set nodeBook = xmlObj.selectSingleNode("//firstname")
  nodeBook.setAttribute "firstname", users_firstname
  Response.Write nodeBook.getAttribute("firstname")
  xmlObj.save(cDir & cFile)
End If

Set xmlObj = Nothing

The problem is that it adds a new section to the XML file instead of updating the value of firstname from Andre to Tristan. The XML looks like this:

<user>
<firstname firstname="Andre6">Andre</firstname>
<lastname>Bruton</lastname>
</user>

What is should look like is:

<user>
<firstname>Tristan</firstname>
<lastname>Bruton</lastname>
</user>

Any idea how I can fix this?

Best regards

Andre

+2  A: 

Instead of using .setAttribute method, you should use .Text property.

If firstname <> users_firstname Then
    Set nodeBook = xmlObj.selectSingleNode("//firstname")
    nodeBook.Text = users_firstname
    Response.Write nodeBook.Text
    xmlObj.save(cDir & cFile)
End If
Mike J