We help IT Professionals succeed at work.

Help with avoiding saving duplicate data in xml file

vcharles
vcharles used Ask the Experts™
on
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.MapPath("~/App_Data/CountryUser.xml"))
        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.MapPath("~/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)).ToList
            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_Data/" & curXYZ & ".xml"))
            Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)


            Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).Elements(curXYZ) 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(C1GridView1.Rows(x).Cells(0).Text) Then
                    For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/Root/CountryUserTable[CountryUser='" & C1GridView1.Rows(x).Cells(0).Text & "']")
                        countryUser_IDA = xnLink.SelectSingleNode("CountryUser_ID").InnerText
                        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(C1GridView1.Rows(x).Cells(0).Text) 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_Data/" & 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.Count)
                If CheckForItemFix.Contains(C1GridView4.Rows(x).Cells(0).Text) Then
                    For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/Root/CountryUserTable[CountryUser='" & C1GridView4.Rows(x).Cells(0).Text & "']")
                        countryUser_IDB = xnLink.SelectSingleNode("CountryUser_ID").InnerText
                        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(C1GridView4.Rows(x).Cells(0).Text) 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_Data/" & 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.MapPath("~/App_Data/LinkSSADB.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(New 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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
These XML files have been a nightmare :-) Do you have to use these files?

> If Not CheckForItemFix.Contains(C1GridView1.Rows(x).Cells(0).Text) Then
What is it doing?

Author

Commented:
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(C1GridView1.Rows(x).Cells(0).Text) 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.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Can you show the code from that function?

Author

Commented:
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(curXYZ) 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(C1GridView1.Rows(x).Cells(0).Text) Then
                    For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/Root/CountryUserTable[CountryUser='" & C1GridView1.Rows(x).Cells(0).Text & "']")
                        countryUser_IDA = xnLink.SelectSingleNode("CountryUser_ID").InnerText
                        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(C1GridView1.Rows(x).Cells(0).Text) 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_Data/" & 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.Count)
                If CheckForItemFix.Contains(C1GridView4.Rows(x).Cells(0).Text) Then
                    For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/Root/CountryUserTable[CountryUser='" & C1GridView4.Rows(x).Cells(0).Text & "']")
                        countryUser_IDB = xnLink.SelectSingleNode("CountryUser_ID").InnerText
                        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(C1GridView4.Rows(x).Cells(0).Text) 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_Data/" & 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)
Most Valuable Expert 2012
Top Expert 2014
Commented:
Have you looked into what CheckForItemFix contains?

Author

Commented:
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(curXYZ) Select el.Value).ToList

xtable is for CountryUsertable
curXYZ is for Countryuser

CountryUser.xml example:

<Root>
<CountryUsertable>
<CountryUse_ID>1<CountryUse_ID>
<CountryUse_ID>CAN<CountryUse_ID>
</CountryUsertable>
<CountryUsertable>
<CountryUse_ID>2<CountryUse_ID>
<CountryUse_ID>DEU<CountryUse_ID>
</CountryUsertable>
</Root>
Most Valuable Expert 2012
Top Expert 2014

Commented:
Set a break point and examine this list to make sure it contains what you think it does.

Author

Commented:
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(curXYZ) Select el.Value).ToList
Msgbox (CheckForItemFix)
Most Valuable Expert 2012
Top Expert 2014

Commented:
Put a grid on page and set this list as data source.

Author

Commented:
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_Data/Item.xml")) '(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.MapPath("~/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)).ToList  
            LargestFixID.Sort()
            num = (LargestFixID.Count() + 1)
           
            Dim domFix As New XmlDocument()
            domFix.Load(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))
            Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)

            Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).Elements(curXYZ) 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(C1GridItemA.Rows(x).Cells(0).Text) Then
                    For Each xnLink As Xml.XmlNode In xdItem.SelectNodes("/Root/ItemTable[Item='" & C1GridItemA.Rows(x).Cells(0).Text & "']")
                        ItemA_ID = xnLink.SelectSingleNode("Item_ID").InnerText
                        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(C1GridItemA.Rows(x).Cells(0).Text) 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_Data/" & 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.MapPath("~/App_Data/Item.xml"))
            Dim MyFixa As XDocument = XDocument.Load(Server.MapPath("~/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)).ToList
            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(C1GridItemR.Rows(x).Cells(0).Text) Then
                    For Each xnLink As Xml.XmlNode In xdItemA.SelectNodes("/Root/ItemTable[Item='" & C1GridItemR.Rows(x).Cells(0).Text & "']")
                        ItemR_ID = xnLink.SelectSingleNode("Item_ID").InnerText
                        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(C1GridItemR.Rows(x).Cells(0).Text) 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_Data/" & 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.MapPath("~/App_Data/Item.xml"))
            num1 = ""
            num2 = ""
            searchIdItemM = ""
            Dim MyFixb As XDocument = XDocument.Load(Server.MapPath("~/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)).ToList
            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(C1GridItemM.Rows(x).Cells(0).Text) Then
                    For Each xnLink As Xml.XmlNode In xdItemB.SelectNodes("/Root/ItemTable[Item='" & C1GridItemM.Rows(x).Cells(0).Text & "']")
                        ItemM_ID = xnLink.SelectSingleNode("Item_ID").InnerText
                        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(C1GridItemM.Rows(x).Cells(0).Text) 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_Data/" & 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.MapPath("~/App_Data/LinkC.xml"))
            Dim LinkID = (From el In MyDocLinkC.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
                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.MapPath("~/App_Data/LinkC.xml"))
            End If
            Exit For
        Next