Avatar of vcharles
vcharles
 asked on

Help with extra element saved in xml file

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>
ASP.NET

Avatar of undefined
Last Comment
Bob Learned

8/22/2022 - Mon
Bob Learned

I don't understand the problem that you are having.
vcharles

ASKER
The problem is I am saving  an additional blank element to my xml file:

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

When I should only be saving:

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

<CountryTable>
    <Country_ID>2</Country_ID>
    <Country />2-888</Country_ID>
  </CountryTable>

Because my listbox only contains 2 rows:
1-1212
2-888
Bob Learned

Can you explain your code, please?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
vcharles

ASKER
Hi,

Below is my explanation of the code with C:

Code:


Dim CheckForCountryFix = (From el In MyFix.Descendants(xtable).Elements(curXYZ) Select el.Value).ToList

C: curXYZ is a string variable containing the xml file (i.e. Country.xml)
C: xtable is the root element (i.e. CountryTable)
C: CheckForCountryFix creates a list of all the data for CountryTable

                Dim u As Integer = 0
                Dim uu As Integer = 0

                     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
     C: Look for data already in Country.xml while looping through the C1ACountry and
        C: If found set CountryA_ID  = Country_ID found in xml file

                                If u > 0 Then
                                    num1 = num1 & "" & CD & ","
                                Else
                                    num1 = CD & ","
                                End If
                                u = u + 1
                            End If
       C: Looping through each row of the list box and concatenating values of CountrA_ID
                        Next
                       End If
                Next

C: Below is same approach accept for it cretaing a string variable for data not found and saving data not found to country.xml

                '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)

Since my lisbox contains:

1-1212
2-888

I should not be save extra element

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

to the xml file.
Bob Learned

Is there a reason to have a Save call within the For loop?

MyFix.Root.Add(xNew)
MyFix.Save(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))

Open in new window

vcharles

ASKER
Yes, I need to save rows not found to my xml file as I loop through the listbox.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question