Avatar of vcharles
vcharles
 asked on

How do you avoid entering duplicate data in xml file?

Hi,

How do I modify the code below to avoid duplicate NSC in my xml file, part  B is an example of NSC.xml. For example when I enter
3333-33-3333-3333 a second time in the Grid, it should not save it again.


Thanks,

Victor

Part A:
Private Sub Save_Click(sender As System.Object, e As System.EventArgs) Handles Save.Click

        Dim NSC_ID As String = Nothing
        xdNSC.Load((Application.StartupPath + "\Data\LinkFiles\NSC.xml"))
        Dim num As Integer
        Dim numx As Integer
        Dim numy As Integer
        Dim num1 As String
        Dim num2 As String
        Dim xyz() As String = New String() {"NSC"}
        'Multiple rows in Grid
        If C1NSC.RowCount <> 0 Then
            For i = 0 To 1
                num1 = ""
                num2 = ""
                num = 0
                numx = 0
                numy = 0
                searchIdNSC = ""
                Dim xid As String
                Dim xtable As String
                ' Dim i As String = "NSC"
                Dim curXYZ = xyz(i)
                Dim MyFix As XDocument = XDocument.Load(Application.StartupPath + "\Data\LinkFiles\" & 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(Application.StartupPath + "\Data\LinkFiles\" & curXYZ & ".xml")
                Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)
                Dim CheckForNSCFix = (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 NSC.xml
                For x As Integer = 0 To C1NSC.RowCount - 1
                    If CheckForNSCFix.Contains(C1NSC.Row) Then
                        For Each xnLink As Xml.XmlNode In xdNSC.SelectNodes("/Root/NSCTable[NSC='" & C1NSC.Item(x)("NSC") & "']")
                            NSC_ID = xnLink.SelectSingleNode("NSC_ID").InnerText
                            If NSC_ID <> "" Then
                                CD = NSC_ID
                                If u > 0 Then
                                    num1 = num1 & "" & CD & ","
                                Else
                                    num1 = CD & ","
                                    ' num1 = CD '& ","
                                End If
                                u = u + 1
                            End If
                        Next
                    End If
                Next
                'Assign ID to data not already in XML file
                If C1NSC.RowCount <> 0 Then
                    For x As Integer = 0 To C1NSC.RowCount - 1
                        Dim theString As String
                        theString = C1NSC.Item(x)("NSC")
                        If theString <> "" Then
                            If System.Text.RegularExpressions.Regex.IsMatch(theString, "^\d{13}$") Then
                                theString = theString.Insert(9, "-").Insert(6, "-").Insert(4, "-")
                            ElseIf Not System.Text.RegularExpressions.Regex.IsMatch(theString, "^\d{4}-\d{2}-\d{3}-\d{4}$") Then
                                MsgBox("NSC(s) must be in the follwoing format [xxx-xx-xxx-xxxx]")
                                Exit Sub
                            End If
                        End If
                        Dim xNew As XElement = New XElement(xtable)
                        xNew.Add(New XElement(xid, num))
                        xNew.Add(New XElement(curXYZ, theString))
                        MyFix.Root.Add(xNew)
                        MyFix.Save(Application.StartupPath + "\Data\LinkFiles\" & curXYZ & ".xml")
                        If uu > 0 Then
                            num2 += num & ","
                        Else
                            num2 = num & ","
                        End If
                        uu = uu + 1
                        num = num + 1
                    Next
                End If
                searchIdNSC = num1 & num2
                Exit For
            Next
        End If
        MsgBox("NSC value " & searchIdNSC)

Open in new window



Part B:
       
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <NSCTable>
    <NSC_ID>0</NSC_ID>
    <NSC></NSC>
  </NSCTable>
  <NSCTable>
    <NSC_ID>2</NSC_ID>
    <NSC>1111-11-111-1111</NSC>
  </NSCTable>
  <NSCTable>
    <NSC_ID>3</NSC_ID>
    <NSC>2222-22-222-2222</NSC>
  </NSCTable>
  <NSCTable>
    <NSC_ID>4</NSC_ID>
    <NSC>3333-33-3333-3333</NSC>
  </NSCTable>
  <NSCTable>
    <NSC_ID>4</NSC_ID>
    <NSC>3333-33-3333-3333</NSC>
  </NSCTable>

</Root>
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
vcharles

8/22/2022 - Mon
aikimark

It looks like you are iterating the nodes near the bottom of your routine.  I would assume that is where you would check for duplicate values.  If you want it to perform faster, you can use a dictionary/hash table object to check for duplicates with one iteration of the nodes.
vcharles

ASKER
Hi,

Can you please send me an example.

Thanks,

V.
aikimark

It will look something like this
Dim oDicUnique As New Dictionary(Of String, Integer)


'add to your iteration of the nodes
If oDicUnique.ContainsKey(theString) Then
  'already output this key, no dups
Else
  oDicUnique.Add(theString, 1)
  'output this unique key
End If

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
vcharles

ASKER
Hi,

Can you please integrate your code with the code I submitted,

Thanks,

V.
aikimark

Can you please integrate your code with the code I submitted,
1. Do you understand your own code?


2. Do you understand the dictionary object?

We are here to help you, not to do your work for you.
vcharles

ASKER
I understand the code I posted, just don't understand how your code fits with it.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
aikimark

Your nodes have unique keys, the <NSC>1111-11-111-1111</NSC> values.

A dictionary object is capable of storing unique keys (string values).  As you iterate your nodes, you check for the existence of the key for the current node before you add it to the XML document.  If you have already encountered the string, you do not add the node.
vcharles

ASKER
Hi,

I'll try further to integrate your code. Thanks for the explanation.

Victor
vcharles

ASKER
Hi,

Do you know a good link with more information on using dictionaries?

Thanks,

Victor
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
aikimark

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
vcharles

ASKER
Thank You very much.

Victor