[SOLVED] Attempts at replacing an XML element duplicates it with both the original AND new values

Issue

I have the follow Sub whose goal is to update or add an XML Tree as needed. As I’m reading the code, it looks like the code is suppose to replace the existing Nodes as desired. However, the code is instead leaving the original alone and adding the values as new. I have checked the inspector and the logic is doing what it’s supposed to be:

Private Sub UpdateAllPlayerXML(strMemberID As String, strMemberName As String, strPlayerFirstName As String, strPlayerLastName As String)
    ' Open the XML AllPlayers.xml file
    Dim xdoc As New XDocument
    xdoc = XDocument.Load(Application.StartupPath + "\AllPlayers.xml")

    Dim target As XElement = xdoc.Descendants("Player").FirstOrDefault(Function(x) x.Element("MembershipNo").Value = strMemberID)

    If strMemberID <> "GUEST99999" And Not String.IsNullOrWhiteSpace(strMemberID) Then
        If IsNothing(target) Then
            ' This should mean we have a NEW element.
            Dim newPlayer As XElement = New XElement("Player")
            newPlayer.Add(New XElement("FirstName", strPlayerFirstName))
            newPlayer.Add(New XElement("LastName", strPlayerLastName))
            newPlayer.Add(New XElement("MembershipNo", strMemberID))
            newPlayer.Add(New XElement("MembershipName", strMemberName))

            xdoc.Element("PlayerList").Add(newPlayer)

        Else
            ' This should mean we are UPDATING an element.
            ' If the incoming name is blank but the existing name is not, DO NOT UPDATE
            target.SetElementValue("FirstName", strPlayerFirstName)
            target.SetElementValue("LastName", strPlayerLastName)
            target.SetElementValue("MembershipNo", strMemberID)
            target.SetElementValue("MembershipName", strMemberName)

        End If

        xdoc.Save(Application.StartupPath + "\AllPlayers.xml")

        ' Since a new team was made, we should update the "Global Player List" on the form.
        LoadPlayersListFromXML()
        lbSearchResults.ClearSelected()
    End If

End Sub

Looking up the resultant XML file, I see that it’s adding a new element and leaving the original alone.


EDIT: As requested:

Solution

I tested it with the code and xml file you provided. I update successfully.

My environment is .NET 4.8, Visual Studio 2019.

The code below is the source you tested.

  Dim xdoc As New System.Xml.Linq.XDocument
      xdoc = System.Xml.Linq.XDocument.Load("AllPlayers.xml")

      Dim strMemberID As String = "0000000446"
      Dim strPlayerFirstName As String = ""
      Dim strPlayerLastName As String = ""
      Dim strMemberName As String = "Test"

      Dim target As XElement = xdoc.Descendants("Player").FirstOrDefault(Function(x) x.Element("MembershipNo").Value = strMemberID)

      If strMemberID <> "GUEST99999" And Not String.IsNullOrWhiteSpace(strMemberID) Then
         If IsNothing(target) Then
            ' This should mean we have a NEW element.
            Dim newPlayer As XElement = New XElement("Player")
            newPlayer.Add(New XElement("FirstName", strPlayerFirstName))
            newPlayer.Add(New XElement("LastName", strPlayerLastName))
            newPlayer.Add(New XElement("MembershipNo", strMemberID))
            newPlayer.Add(New XElement("MembershipName", strMemberName))

            xdoc.Element("PlayerList").Add(newPlayer)

         Else
            ' This should mean we are UPDATING an element.
            ' If the incoming name is blank but the existing name is not, DO NOT UPDATE
            target.SetElementValue("FirstName", strPlayerFirstName)
            target.SetElementValue("LastName", strPlayerLastName)
            target.SetElementValue("MembershipNo", strMemberID)
            target.SetElementValue("MembershipName", strMemberName)

         End If

         xdoc.Save("AllPlayers.xml")

If you find a solution, please let me know too.

Answered By – Think2826

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.