troubleshooting Question

Help with extra element saved in xml file

Avatar of vcharles
vcharles asked on
ASP.NET
7 Comments1 Solution437 ViewsLast Modified:
Hello,

Can you please help me figure out why I am getting an extra element when saving data from a listbox to my xml file. My listbox contains:

1-1212
2-888

But when I save the data I receive the extra element below:

 <CountryTable>
    <Country_ID>2</Country_ID>
    <Country />
  </CountryTable>

The xml file, initially contained:

<CountryTable>
    <Country_ID>1</Country_ID>
    <Country>1-1212</Country>
  </CountryTable>

Code:

Dim CheckForCountryFix = (From el In MyFix.Descendants(xtable).Elements(curXYZ) Select el.Value).ToList
                Dim u As Integer = 0
                Dim uu As Integer = 0

                'Look for data already in Country.xml
                'If i = 0 Then
                For x As Integer = 0 To C1CountryA.Items.Count - 1
                    If CheckForCountryFix.Contains(C1CountryA.Items(x).Text) Then
                        For Each xnLink As Xml.XmlNode In xdCountry.SelectNodes("/Root/CountryTable[Country='" & C1CountryA.Items(x).Text & "']")
                            CountryA_ID = xnLink.SelectSingleNode("Country_ID").InnerText
                            If CountryA_ID <> "" Then
                                CD = CountryA_ID
                                If u > 0 Then
                                    num1 = num1 & "" & CD & ","
                                Else
                                    num1 = CD & ","
                                End If
                                u = u + 1
                            End If
                        Next
                       End If
                Next
                'Assign ID to data not already in XML file
                For x As Integer = 0 To C1CountryA.Items.Count - 1
                    If Not CheckForCountryFix.Contains(C1CountryA.Items(x).Text) Then
                        Dim xNew As XElement = New XElement(xtable)
                        xNew.Add(New XElement(xid, num))
                        xNew.Add(New XElement(curXYZ, C1CountryA.Items(x).Text))
                        MyFix.Root.Add(xNew)
                        MyFix.Save(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))
                        If uu > 0 Then
                            num2 += num & ","
                        Else
                            num2 = num & ","
                        End If
                        uu = uu + 1
                        num = num + 1
                    End If
                Next
                searchIdCountryA = num1 & num2
                MsgBox("Save Data " & searchIdCountryA)

 ‘*********************************
XML File:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <CountryTable>
    <Country_ID>1</Country_ID>
    <Country>1-1212</Country>
  </CountryTable>
  <CountryTable>
    <Country_ID>2</Country_ID>
    <Country>2-888</Country>
  </CountryTable>
  <CountryTable>
    <Country_ID>2</Country_ID>
    <Country />
  </CountryTable>
</Root>
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros