We help IT Professionals succeed at work.
Get Started

How do you avoid entering duplicate data in xml file?

455 Views
Last Modified: 2014-05-13
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>
Comment
Watch Question
Social distance; Wear a mask; Get vaccinated
CERTIFIED EXPERT
Top Expert 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE