Search code examples
xmlexcelvbamsxml

MSXML Remove Node Not Behaving As Expected


I am trying to delete a node from the following XML - Data below is only representative of the acutal data:

<StaffMembers>
    <Staff Name="Test1" Date="Date1"/>
    <Staff Name="Test2" Date="Date2"/>
</StaffMembers>

My code is as follows:

Sub DeleteRecord(strName as string, strDate as string)

'Load Document

    Set xList = xDoc.SelectNodes("//StaffMembers/")
    for each xNode in xList
             If xNode.Attributes.Length > 0 And xNode.Attributes.getNamedItem("Date").NodeValue = strDate Then
                 xnode.parentnode.removechild xnode
                 exit for
             End if
    next xNode

'Save Document

End Sub

Resulting XML - as you can see, it is not complete:

<StaffMembers>
    <Staff Name="Test1" Date="Date1"/>
    <Staff Name="Tes

The correct node is selected, but it is only partially deleted. If I open the resulting XML in Notepad, some of the text of the node remains. Can anyone explain this?

Edit:

I have tried removing each Attribute individually before removing the Element. This suffers from the same problem.

Thanks


Solution

  • Thanks for the replies. I've managed to narrow the problem down to having a slow network connection. The corruption does not occur when saving to a local folder.

    Thanks