Victor Charles
asked on
Help with avoiding saving duplicate data in xml file
Hello,
I am entering data in GridView1 and GridView4, the goal is to only copy unique values to CountryUser.xml, problem with my code is I am copying duplicate data entered in both Grids. How do I modify my code below to avoid this problem?
For example when I enter
BEL
CAN
DEU
FRA
In GridView1
and enter
USA
BEL
CAN
DEU
GRC
in GridView4
My countryUser.xml shows:
BEL
CAN
DEU
FRA
USA
BEL
CAN
DEU
GRC
However, I would like it to show
BEL
CAN
DEU
FRA
USA
GRC
Code:
Dim xdReceiver As New Xml.XmlDocument
xdReceiver.Load(Server.Map Path("~/Ap p_Data/Cou ntryUser.x ml"))
Dim searchIdCountryOrigin As String = Nothing
Dim searchIdCountryUser As String = Nothing
Dim xyz() As String = New String() {"CountryUser"}
Dim num As Integer
Dim numa As String
Dim numb As String
Dim num1 As String
Dim num2 As String
Dim countryUser_IDA As String
Dim countryUser_IDB As String
countryUser_IDA = ""
For i = 0 To 1
num1 = ""
num2 = ""
numa = ""
num = 0
Dim curXYZ = xyz(i)
Dim MyFix As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/" & curXYZ & ".xml"))
Dim xid As String
xid = curXYZ & "_ID"
Dim LargestFixID = (From el In MyFix.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToLi st
LargestFixID.Sort()
num = (LargestFixID.Count() + 1)
Dim xtable As String
xtable = curXYZ & "Table"
Dim searchFix As String = C1GridView1.Rows(x).Cells( 0).Text
Dim domFix As New XmlDocument()
domFix.Load(Server.MapPath ("~/App_Da ta/" & curXYZ & ".xml"))
Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)
Dim CheckForItemFix = (From el In MyFix.Descendants(xtable). Elements(c urXYZ) Select el.Value).ToList
Dim u As Integer = 0
Dim uu As Integer = 0
For x As Integer = 0 To C1GridView1.Rows.Count - 1
If CheckForItemFix.Contains(C 1GridView1 .Rows(x).C ells(0).Te xt) Then
For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/R oot/Countr yUserTable [CountryUs er='" & C1GridView1.Rows(x).Cells( 0).Text & "']")
countryUser_IDA = xnLink.SelectSingleNode("C ountryUser _ID").Inne rText
If countryUser_IDA <> "" Then
CD = countryUser_IDA
If u > 0 Then
num1 = num1 & "" & CD & ","
Else
num1 = CD & ","
End If
u = u + 1
End If
Next
End If
Next
For x As Integer = 0 To C1GridView1.Rows.Count - 1
If Not CheckForItemFix.Contains(C 1GridView1 .Rows(x).C ells(0).Te xt) Then
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, num))
xNew.Add(New XElement(curXYZ, C1GridView1.Rows(x).Cells( 0).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
numa = num1 & num2
MsgBox(numa)
'************************* ********** ********** ********** ********** ********** ********** ********** **
num1 = ""
num2 = ""
numb = ""
' num = 0
For x As Integer = 0 To C1GridView4.Rows.Count - 1
MsgBox("count")
MsgBox(C1GridView4.Rows.Co unt)
If CheckForItemFix.Contains(C 1GridView4 .Rows(x).C ells(0).Te xt) Then
For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/R oot/Countr yUserTable [CountryUs er='" & C1GridView4.Rows(x).Cells( 0).Text & "']")
countryUser_IDB = xnLink.SelectSingleNode("C ountryUser _ID").Inne rText
If countryUser_IDB <> "" Then
CD = countryUser_IDB
If u > 0 Then
num1 = num1 & "" & CD & ","
Else
num1 = CD & ","
End If
u = u + 1
End If
Next
End If
Next
For x As Integer = 0 To C1GridView4.Rows.Count - 1
If Not CheckForItemFix.Contains(C 1GridView4 .Rows(x).C ells(0).Te xt) Then
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, num))
xNew.Add(New XElement(curXYZ, C1GridView4.Rows(x).Cells( 0).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
numb = num1 & num2
MsgBox(numb)
'************************* ********** ********** ********** ********** ********** ********** ********** ******
Dim MyDocLinkSSADB As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/Link SSADB.xml" ))
Dim LinkID = (From el In MyDocLinkSSADB.Descendants ("LinkA") Select CInt(el.<LinkAID>.Value)). ToList
LinkID.Sort()
Dim xNewLink As XElement = New XElement("LinkA")
xNewLink.Add(New XElement("LinkAID", (LinkID.Last() + 1)))
If i = 0 Then
MyDocLinkSSADB.Root.Add(Ne w XElement("LinkA", New XElement("LinkAID", (LinkID.Last() + 1)), New XElement("CountryOrigin_ID ", numb), New XElement("CountryUser_ID", numa)))
MyDocLinkSSADB.Save(Server .MapPath(" ~/App_Data /LinkSSADB .xml"))
End If
Next
I am entering data in GridView1 and GridView4, the goal is to only copy unique values to CountryUser.xml, problem with my code is I am copying duplicate data entered in both Grids. How do I modify my code below to avoid this problem?
For example when I enter
BEL
CAN
DEU
FRA
In GridView1
and enter
USA
BEL
CAN
DEU
GRC
in GridView4
My countryUser.xml shows:
BEL
CAN
DEU
FRA
USA
BEL
CAN
DEU
GRC
However, I would like it to show
BEL
CAN
DEU
FRA
USA
GRC
Code:
Dim xdReceiver As New Xml.XmlDocument
xdReceiver.Load(Server.Map
Dim searchIdCountryOrigin As String = Nothing
Dim searchIdCountryUser As String = Nothing
Dim xyz() As String = New String() {"CountryUser"}
Dim num As Integer
Dim numa As String
Dim numb As String
Dim num1 As String
Dim num2 As String
Dim countryUser_IDA As String
Dim countryUser_IDB As String
countryUser_IDA = ""
For i = 0 To 1
num1 = ""
num2 = ""
numa = ""
num = 0
Dim curXYZ = xyz(i)
Dim MyFix As XDocument = XDocument.Load(Server.MapP
Dim xid As String
xid = curXYZ & "_ID"
Dim LargestFixID = (From el In MyFix.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToLi
LargestFixID.Sort()
num = (LargestFixID.Count() + 1)
Dim xtable As String
xtable = curXYZ & "Table"
Dim searchFix As String = C1GridView1.Rows(x).Cells(
Dim domFix As New XmlDocument()
domFix.Load(Server.MapPath
Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)
Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).
Dim u As Integer = 0
Dim uu As Integer = 0
For x As Integer = 0 To C1GridView1.Rows.Count - 1
If CheckForItemFix.Contains(C
For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/R
countryUser_IDA = xnLink.SelectSingleNode("C
If countryUser_IDA <> "" Then
CD = countryUser_IDA
If u > 0 Then
num1 = num1 & "" & CD & ","
Else
num1 = CD & ","
End If
u = u + 1
End If
Next
End If
Next
For x As Integer = 0 To C1GridView1.Rows.Count - 1
If Not CheckForItemFix.Contains(C
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, num))
xNew.Add(New XElement(curXYZ, C1GridView1.Rows(x).Cells(
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
numa = num1 & num2
MsgBox(numa)
'*************************
num1 = ""
num2 = ""
numb = ""
' num = 0
For x As Integer = 0 To C1GridView4.Rows.Count - 1
MsgBox("count")
MsgBox(C1GridView4.Rows.Co
If CheckForItemFix.Contains(C
For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/R
countryUser_IDB = xnLink.SelectSingleNode("C
If countryUser_IDB <> "" Then
CD = countryUser_IDB
If u > 0 Then
num1 = num1 & "" & CD & ","
Else
num1 = CD & ","
End If
u = u + 1
End If
Next
End If
Next
For x As Integer = 0 To C1GridView4.Rows.Count - 1
If Not CheckForItemFix.Contains(C
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, num))
xNew.Add(New XElement(curXYZ, C1GridView4.Rows(x).Cells(
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
numb = num1 & num2
MsgBox(numb)
'*************************
Dim MyDocLinkSSADB As XDocument = XDocument.Load(Server.MapP
Dim LinkID = (From el In MyDocLinkSSADB.Descendants
LinkID.Sort()
Dim xNewLink As XElement = New XElement("LinkA")
xNewLink.Add(New XElement("LinkAID", (LinkID.Last() + 1)))
If i = 0 Then
MyDocLinkSSADB.Root.Add(Ne
MyDocLinkSSADB.Save(Server
End If
Next
ASKER
This is the design of the XML files, do you have a better deisgn that would not not have a great impact on my my project, I more than half way to completion.
If Not CheckForItemFix.Contains(C 1GridView1 .Rows(x).C ells(0).Te xt) Then
It checks to see if C1GridView1.text alraedy exist in the xml file. If it does not
the following code is supposed to:
1. Create a new ID
2. Save the data from C1GridView1.text to the xml file.
The second part of the code (If CheckForItemFix.Contains) searches for if the text from C1Gridview already exist, if yes, it is supposed to take the existing ID from the xml file.
I can't figure out why even when text from C1GridView1 already exist, it is still copying it to the xml file.
If Not CheckForItemFix.Contains(C
It checks to see if C1GridView1.text alraedy exist in the xml file. If it does not
the following code is supposed to:
1. Create a new ID
2. Save the data from C1GridView1.text to the xml file.
The second part of the code (If CheckForItemFix.Contains) searches for if the text from C1Gridview already exist, if yes, it is supposed to take the existing ID from the xml file.
I can't figure out why even when text from C1GridView1 already exist, it is still copying it to the xml file.
Can you show the code from that function?
ASKER
This part of the code below (Part A) works ok, it the second part (Part B) that is copying duplicates to the CountryUser.xml file.
Dim CheckForItemFix = (From el In MyFix.Descendants(xtable). Elements(c urXYZ) Select el.Value).ToList
Dim u As Integer = 0
Dim uu As Integer = 0
For x As Integer = 0 To C1GridView1.Rows.Count - 1
If CheckForItemFix.Contains(C 1GridView1 .Rows(x).C ells(0).Te xt) Then
For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/R oot/Countr yUserTable [CountryUs er='" & C1GridView1.Rows(x).Cells( 0).Text & "']")
countryUser_IDA = xnLink.SelectSingleNode("C ountryUser _ID").Inne rText
If countryUser_IDA <> "" Then
CD = countryUser_IDA
If u > 0 Then
num1 = num1 & "" & CD & ","
Else
num1 = CD & ","
End If
u = u + 1
End If
Next
End If
Next
For x As Integer = 0 To C1GridView1.Rows.Count - 1
If Not CheckForItemFix.Contains(C 1GridView1 .Rows(x).C ells(0).Te xt) Then
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, num))
xNew.Add(New XElement(curXYZ, C1GridView1.Rows(x).Cells( 0).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
numa = num1 & num2
MsgBox(numa)
Part B:
num1 = ""
num2 = ""
numb = ""
' num = 0
For x As Integer = 0 To C1GridView4.Rows.Count - 1
MsgBox("count")
MsgBox(C1GridView4.Rows.Co unt)
If CheckForItemFix.Contains(C 1GridView4 .Rows(x).C ells(0).Te xt) Then
For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/R oot/Countr yUserTable [CountryUs er='" & C1GridView4.Rows(x).Cells( 0).Text & "']")
countryUser_IDB = xnLink.SelectSingleNode("C ountryUser _ID").Inne rText
If countryUser_IDB <> "" Then
CD = countryUser_IDB
If u > 0 Then
num1 = num1 & "" & CD & ","
Else
num1 = CD & ","
End If
u = u + 1
End If
Next
End If
Next
For x As Integer = 0 To C1GridView4.Rows.Count - 1
If Not CheckForItemFix.Contains(C 1GridView4 .Rows(x).C ells(0).Te xt) Then
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, num))
xNew.Add(New XElement(curXYZ, C1GridView4.Rows(x).Cells( 0).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
numb = num1 & num2
MsgBox(numb)
Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).
Dim u As Integer = 0
Dim uu As Integer = 0
For x As Integer = 0 To C1GridView1.Rows.Count - 1
If CheckForItemFix.Contains(C
For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/R
countryUser_IDA = xnLink.SelectSingleNode("C
If countryUser_IDA <> "" Then
CD = countryUser_IDA
If u > 0 Then
num1 = num1 & "" & CD & ","
Else
num1 = CD & ","
End If
u = u + 1
End If
Next
End If
Next
For x As Integer = 0 To C1GridView1.Rows.Count - 1
If Not CheckForItemFix.Contains(C
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, num))
xNew.Add(New XElement(curXYZ, C1GridView1.Rows(x).Cells(
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
numa = num1 & num2
MsgBox(numa)
Part B:
num1 = ""
num2 = ""
numb = ""
' num = 0
For x As Integer = 0 To C1GridView4.Rows.Count - 1
MsgBox("count")
MsgBox(C1GridView4.Rows.Co
If CheckForItemFix.Contains(C
For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/R
countryUser_IDB = xnLink.SelectSingleNode("C
If countryUser_IDB <> "" Then
CD = countryUser_IDB
If u > 0 Then
num1 = num1 & "" & CD & ","
Else
num1 = CD & ","
End If
u = u + 1
End If
Next
End If
Next
For x As Integer = 0 To C1GridView4.Rows.Count - 1
If Not CheckForItemFix.Contains(C
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, num))
xNew.Add(New XElement(curXYZ, C1GridView4.Rows(x).Cells(
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
numb = num1 & num2
MsgBox(numb)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have not, but based on the code below, it should contain a list CountryUser from CountryUser.xml.
Dim CheckForItemFix = (From el In MyFix.Descendants(xtable). Elements(c urXYZ) Select el.Value).ToList
xtable is for CountryUsertable
curXYZ is for Countryuser
CountryUser.xml example:
<Root>
<CountryUsertable>
<CountryUse_ID>1<CountryUs e_ID>
<CountryUse_ID>CAN<Country Use_ID>
</CountryUsertable>
<CountryUsertable>
<CountryUse_ID>2<CountryUs e_ID>
<CountryUse_ID>DEU<Country Use_ID>
</CountryUsertable>
</Root>
Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).
xtable is for CountryUsertable
curXYZ is for Countryuser
CountryUser.xml example:
<Root>
<CountryUsertable>
<CountryUse_ID>1<CountryUs
<CountryUse_ID>CAN<Country
</CountryUsertable>
<CountryUsertable>
<CountryUse_ID>2<CountryUs
<CountryUse_ID>DEU<Country
</CountryUsertable>
</Root>
Set a break point and examine this list to make sure it contains what you think it does.
ASKER
How do I set a break to see the values? I tried setting a message box box I get an error message "Cn not convert to String"
Dim CheckForItemFix = (From el In MyFix.Descendants(xtable). Elements(c urXYZ) Select el.Value).ToList
Msgbox (CheckForItemFix)
Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).
Msgbox (CheckForItemFix)
Put a grid on page and set this list as data source.
ASKER
Hi,
I solved the problem. Thanks for your help.
I have another related question, please take a look at the code below, it works but I would like to improve it, do you see a way I can significantly improve it, I need to do do for about 20 controls and I'm concerened speed my be an issue.
Basically I'm trying to save data entered in three grids to LinkC.xml, if the data already use existing ID if nor save it o Item.xml and create a new ID to save to LinkC.xml.
I only include Item.xml in Dim xyz() As String = New String() {"Item"} but the project with have about 20 different xml files.
Dim xdItem As New Xml.XmlDocument '(1)
Dim xdItemA As New Xml.XmlDocument
Dim xdItemB As New Xml.XmlDocument
xdItem.Load(Server.MapPath ("~/App_Da ta/Item.xm l")) '(2)
Dim searchIdCountryOrigin As String = Nothing
Dim searchIdCountryUser As String = Nothing
Dim searchIdNSN As String = Nothing
Dim searchIdItemA As String
Dim searchIdItemR As String
Dim searchIdItemM As String
Dim num As Integer
Dim numx As Integer
Dim numy As Integer
Dim num1 As String
Dim num2 As String
Dim countryUser_IDA As String = Nothing
Dim countryUser_IDB As String = Nothing
Dim NSN_ID As String = Nothing
Dim ItemA_ID As String
Dim ItemR_ID As String
Dim ItemM_ID As String
'************************* ********** ******LOOP Begins******************** ********** ********** ********** *8
Dim xyz() As String = New String() {"Item"}
For i = 0 To 1
num1 = ""
num2 = ""
num = 0
numx = 0
numy = 0
searchIdItemA = ""
Dim xid As String
Dim xtable As String
Dim curXYZ = xyz(i)
Dim MyFix As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/" & curXYZ & ".xml"))
xid = curXYZ & "_ID"
xtable = curXYZ & "Table"
'Sort ID in each xml file
Dim LargestFixID = (From el In MyFix.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToLi st
LargestFixID.Sort()
num = (LargestFixID.Count() + 1)
Dim domFix As New XmlDocument()
domFix.Load(Server.MapPath ("~/App_Da ta/" & curXYZ & ".xml"))
Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)
Dim CheckForItemFix = (From el In MyFix.Descendants(xtable). Elements(c urXYZ) Select el.Value).ToList
Dim u As Integer = 0
Dim uu As Integer = 0
'***********Looping through all controls with multiple lines********************* ********** ********** ********** ****
'Look for data already in Item.xml
For x As Integer = 0 To C1GridItemA.Rows.Count - 1
If CheckForItemFix.Contains(C 1GridItemA .Rows(x).C ells(0).Te xt) Then
For Each xnLink As Xml.XmlNode In xdItem.SelectNodes("/Root/ ItemTable[ Item='" & C1GridItemA.Rows(x).Cells( 0).Text & "']")
ItemA_ID = xnLink.SelectSingleNode("I tem_ID").I nnerText
If ItemA_ID <> "" Then
CD = ItemA_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 C1GridItemA.Rows.Count - 1
If Not CheckForItemFix.Contains(C 1GridItemA .Rows(x).C ells(0).Te xt) Then
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, num))
xNew.Add(New XElement(curXYZ, C1GridItemA.Rows(x).Cells( 0).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
searchIdItemA = num1 & num2
'************************* ********** ********** ********** ********** ********** ********** ********** ********** *****
num1 = ""
num2 = ""
xdItemA.Load(Server.MapPat h("~/App_D ata/Item.x ml"))
Dim MyFixa As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/" & curXYZ & ".xml"))
Dim CheckForItemFixa = (From el In MyFixa.Descendants(xtable) .Elements( curXYZ) Select el.Value).ToList
Dim LargestFixIDA = (From el In MyFixa.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToLi st
LargestFixIDA.Sort()
numx = (LargestFixIDA.Count() + 1)
searchIdItemR = ""
For x As Integer = 0 To C1GridItemR.Rows.Count - 1
'Look for data already in XML file
If CheckForItemFixa.Contains( C1GridItem R.Rows(x). Cells(0).T ext) Then
For Each xnLink As Xml.XmlNode In xdItemA.SelectNodes("/Root /ItemTable [Item='" & C1GridItemR.Rows(x).Cells( 0).Text & "']")
ItemR_ID = xnLink.SelectSingleNode("I tem_ID").I nnerText
If ItemR_ID <> "" Then
CD = ItemR_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 C1GridItemR.Rows.Count - 1
If Not CheckForItemFixa.Contains( C1GridItem R.Rows(x). Cells(0).T ext) Then
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, numx))
xNew.Add(New XElement(curXYZ, C1GridItemR.Rows(x).Cells( 0).Text))
MyFixa.Root.Add(xNew)
MyFixa.Save(Server.MapPath ("~/App_Da ta/" & curXYZ & ".xml"))
If uu > 0 Then
num2 += numx & ","
Else
num2 = numx & ","
End If
uu = uu + 1
numx = numx + 1
End If
Next
searchIdItemR = num1 & num2
'************************* ********** ********** ********** ********** ********** ********** ********** ********** ********** *
xdItemB.Load(Server.MapPat h("~/App_D ata/Item.x ml"))
num1 = ""
num2 = ""
searchIdItemM = ""
Dim MyFixb As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/" & curXYZ & ".xml"))
Dim CheckForItemFixb = (From el In MyFixb.Descendants(xtable) .Elements( curXYZ) Select el.Value).ToList
Dim LargestFixIDB = (From el In MyFixb.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToLi st
LargestFixIDB.Sort()
numy = (LargestFixIDB.Count() + 1)
For x As Integer = 0 To C1GridItemM.Rows.Count - 1
'Look for data alraedy in XML file
If CheckForItemFixb.Contains( C1GridItem M.Rows(x). Cells(0).T ext) Then
For Each xnLink As Xml.XmlNode In xdItemB.SelectNodes("/Root /ItemTable [Item='" & C1GridItemM.Rows(x).Cells( 0).Text & "']")
ItemM_ID = xnLink.SelectSingleNode("I tem_ID").I nnerText
If ItemM_ID <> "" Then
CD = ItemM_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 C1GridItemM.Rows.Count - 1
If Not CheckForItemFixb.Contains( C1GridItem M.Rows(x). Cells(0).T ext) Then
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, numy))
xNew.Add(New XElement(curXYZ, C1GridItemM.Rows(x).Cells( 0).Text))
MyFixb.Root.Add(xNew)
MyFixb.Save(Server.MapPath ("~/App_Da ta/" & curXYZ & ".xml"))
If uu > 0 Then
num2 += numy & ","
Else
num2 = numy & ","
End If
uu = uu + 1
numy = numy + 1
End If
Next
searchIdItemM = num1 & num2
Dim MyDocLinkC As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/Link C.xml"))
Dim LinkID = (From el In MyDocLinkC.Descendants("Li nkA") Select CInt(el.<LinkAID>.Value)). ToList
LinkID.Sort()
Dim xNewLink As XElement = New XElement("LinkA")
xNewLink.Add(New XElement("LinkAID", (LinkID.Last() + 1)))
If i = 0 Then
MyDocLinkC.Root.Add(New XElement("LinkA", New XElement("LinkAID", (LinkID.Last() + 1)), New XElement("ItemA_ID", searchIdItemA), New XElement("ItemR_ID", searchIdItemR), New XElement("ItemM_ID", searchIdItemM)))
MyDocLinkC.Save(Server.Map Path("~/Ap p_Data/Lin kC.xml"))
End If
Exit For
Next
I solved the problem. Thanks for your help.
I have another related question, please take a look at the code below, it works but I would like to improve it, do you see a way I can significantly improve it, I need to do do for about 20 controls and I'm concerened speed my be an issue.
Basically I'm trying to save data entered in three grids to LinkC.xml, if the data already use existing ID if nor save it o Item.xml and create a new ID to save to LinkC.xml.
I only include Item.xml in Dim xyz() As String = New String() {"Item"} but the project with have about 20 different xml files.
Dim xdItem As New Xml.XmlDocument '(1)
Dim xdItemA As New Xml.XmlDocument
Dim xdItemB As New Xml.XmlDocument
xdItem.Load(Server.MapPath
Dim searchIdCountryOrigin As String = Nothing
Dim searchIdCountryUser As String = Nothing
Dim searchIdNSN As String = Nothing
Dim searchIdItemA As String
Dim searchIdItemR As String
Dim searchIdItemM As String
Dim num As Integer
Dim numx As Integer
Dim numy As Integer
Dim num1 As String
Dim num2 As String
Dim countryUser_IDA As String = Nothing
Dim countryUser_IDB As String = Nothing
Dim NSN_ID As String = Nothing
Dim ItemA_ID As String
Dim ItemR_ID As String
Dim ItemM_ID As String
'*************************
Dim xyz() As String = New String() {"Item"}
For i = 0 To 1
num1 = ""
num2 = ""
num = 0
numx = 0
numy = 0
searchIdItemA = ""
Dim xid As String
Dim xtable As String
Dim curXYZ = xyz(i)
Dim MyFix As XDocument = XDocument.Load(Server.MapP
xid = curXYZ & "_ID"
xtable = curXYZ & "Table"
'Sort ID in each xml file
Dim LargestFixID = (From el In MyFix.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToLi
LargestFixID.Sort()
num = (LargestFixID.Count() + 1)
Dim domFix As New XmlDocument()
domFix.Load(Server.MapPath
Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)
Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).
Dim u As Integer = 0
Dim uu As Integer = 0
'***********Looping through all controls with multiple lines*********************
'Look for data already in Item.xml
For x As Integer = 0 To C1GridItemA.Rows.Count - 1
If CheckForItemFix.Contains(C
For Each xnLink As Xml.XmlNode In xdItem.SelectNodes("/Root/
ItemA_ID = xnLink.SelectSingleNode("I
If ItemA_ID <> "" Then
CD = ItemA_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 C1GridItemA.Rows.Count - 1
If Not CheckForItemFix.Contains(C
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, num))
xNew.Add(New XElement(curXYZ, C1GridItemA.Rows(x).Cells(
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
searchIdItemA = num1 & num2
'*************************
num1 = ""
num2 = ""
xdItemA.Load(Server.MapPat
Dim MyFixa As XDocument = XDocument.Load(Server.MapP
Dim CheckForItemFixa = (From el In MyFixa.Descendants(xtable)
Dim LargestFixIDA = (From el In MyFixa.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToLi
LargestFixIDA.Sort()
numx = (LargestFixIDA.Count() + 1)
searchIdItemR = ""
For x As Integer = 0 To C1GridItemR.Rows.Count - 1
'Look for data already in XML file
If CheckForItemFixa.Contains(
For Each xnLink As Xml.XmlNode In xdItemA.SelectNodes("/Root
ItemR_ID = xnLink.SelectSingleNode("I
If ItemR_ID <> "" Then
CD = ItemR_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 C1GridItemR.Rows.Count - 1
If Not CheckForItemFixa.Contains(
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, numx))
xNew.Add(New XElement(curXYZ, C1GridItemR.Rows(x).Cells(
MyFixa.Root.Add(xNew)
MyFixa.Save(Server.MapPath
If uu > 0 Then
num2 += numx & ","
Else
num2 = numx & ","
End If
uu = uu + 1
numx = numx + 1
End If
Next
searchIdItemR = num1 & num2
'*************************
xdItemB.Load(Server.MapPat
num1 = ""
num2 = ""
searchIdItemM = ""
Dim MyFixb As XDocument = XDocument.Load(Server.MapP
Dim CheckForItemFixb = (From el In MyFixb.Descendants(xtable)
Dim LargestFixIDB = (From el In MyFixb.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToLi
LargestFixIDB.Sort()
numy = (LargestFixIDB.Count() + 1)
For x As Integer = 0 To C1GridItemM.Rows.Count - 1
'Look for data alraedy in XML file
If CheckForItemFixb.Contains(
For Each xnLink As Xml.XmlNode In xdItemB.SelectNodes("/Root
ItemM_ID = xnLink.SelectSingleNode("I
If ItemM_ID <> "" Then
CD = ItemM_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 C1GridItemM.Rows.Count - 1
If Not CheckForItemFixb.Contains(
Dim xNew As XElement = New XElement(xtable)
xNew.Add(New XElement(xid, numy))
xNew.Add(New XElement(curXYZ, C1GridItemM.Rows(x).Cells(
MyFixb.Root.Add(xNew)
MyFixb.Save(Server.MapPath
If uu > 0 Then
num2 += numy & ","
Else
num2 = numy & ","
End If
uu = uu + 1
numy = numy + 1
End If
Next
searchIdItemM = num1 & num2
Dim MyDocLinkC As XDocument = XDocument.Load(Server.MapP
Dim LinkID = (From el In MyDocLinkC.Descendants("Li
LinkID.Sort()
Dim xNewLink As XElement = New XElement("LinkA")
xNewLink.Add(New XElement("LinkAID", (LinkID.Last() + 1)))
If i = 0 Then
MyDocLinkC.Root.Add(New XElement("LinkA", New XElement("LinkAID", (LinkID.Last() + 1)), New XElement("ItemA_ID", searchIdItemA), New XElement("ItemR_ID", searchIdItemR), New XElement("ItemM_ID", searchIdItemM)))
MyDocLinkC.Save(Server.Map
End If
Exit For
Next
> If Not CheckForItemFix.Contains(C
What is it doing?