Victor Charles
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(c urXYZ) 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.Contain s(C1Countr yA.Items(x ).Text) Then
For Each xnLink As Xml.XmlNode In xdCountry.SelectNodes("/Ro ot/Country Table[Coun try='" & C1CountryA.Items(x).Text & "']")
CountryA_ID = xnLink.SelectSingleNode("C ountry_ID" ).InnerTex t
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.Contain s(C1Countr yA.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_Dat a/" & 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>
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).
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.Contain
For Each xnLink As Xml.XmlNode In xdCountry.SelectNodes("/Ro
CountryA_ID = xnLink.SelectSingleNode("C
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.Contain
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(
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>
I don't understand the problem that you are having.
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
<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
Can you explain your code, please?
ASKER
Hi,
Below is my explanation of the code with C:
Code:
Dim CheckForCountryFix = (From el In MyFix.Descendants(xtable). Elements(c urXYZ) 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.Contain s(C1Countr yA.Items(x ).Text) Then
For Each xnLink As Xml.XmlNode In xdCountry.SelectNodes
("/Root/CountryTable[Count ry='" & C1CountryA.Items(x).Text & "']")
CountryA_ID = xnLink.SelectSingleNode("C ountry_ID" ).InnerTex t
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.Contain s(C1Countr yA.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_Dat a/" & 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.
Below is my explanation of the code with C:
Code:
Dim CheckForCountryFix = (From el In MyFix.Descendants(xtable).
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.Contain
For Each xnLink As Xml.XmlNode In xdCountry.SelectNodes
("/Root/CountryTable[Count
CountryA_ID = xnLink.SelectSingleNode("C
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.Contain
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(
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.
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"))
ASKER
Yes, I need to save rows not found to my xml file as I loop through the listbox.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.