Victor Charles
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:
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</NS C>
</NSCTable>
<NSCTable>
<NSC_ID>4</NSC_ID>
<NSC>3333-33-3333-3333</NS C>
</NSCTable>
</Root>
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)
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</NS
</NSCTable>
<NSCTable>
<NSC_ID>4</NSC_ID>
<NSC>3333-33-3333-3333</NS
</NSCTable>
</Root>
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.
ASKER
Hi,
Can you please send me an example.
Thanks,
V.
Can you please send me an example.
Thanks,
V.
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
ASKER
Hi,
Can you please integrate your code with the code I submitted,
Thanks,
V.
Can you please integrate your code with the code I submitted,
Thanks,
V.
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.
ASKER
I understand the code I posted, just don't understand how your code fits with it.
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.
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.
ASKER
Hi,
I'll try further to integrate your code. Thanks for the explanation.
Victor
I'll try further to integrate your code. Thanks for the explanation.
Victor
ASKER
Hi,
Do you know a good link with more information on using dictionaries?
Thanks,
Victor
Do you know a good link with more information on using dictionaries?
Thanks,
Victor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You very much.
Victor
Victor