[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 543
  • Last Modified:

Help with saving duplicate data found to an xml file

Hi,

The code below checks successfully for duplicates but I need to use the existing ID of the matching record insated of creating a new record. I included a messagebox to tell the user the data entered already exist, but instead of the messagebox, I would prefer to save the existing ID (ReceiverID) of the matching record to another xml file.

 'Receiver()
        Dim MyDocReceiver As XDocument = XDocument.Load(Application.StartupPath & "/Receiver.xml")
        Dim LargestReceiverID = (From el In MyDocReceiver.Descendants("Receiver") Select CInt(el.<ReceiverID>.Value)).ToList
        LargestReceiverID.Sort()
        Dim CheckForItem1 = (From el In MyDocReceiver.Descendants("Receiver").Elements("Name") Select el.Value).ToList
        If Not CheckForItem1.Contains(Me.Textbox2.Text.ToString) Then
            Dim xNew1 As XElement = New XElement("Receiver")
            xNew1.Add(New XElement("ReceiverID", (LargestReceiverID.Last() + 1)))
            xNew1.Add(New XElement("Name", Me.Textbox2.Text.ToString))
            MyDocReceiver.Root.Add(xNew1)
            MyDocReceiver.Save(Application.StartupPath & "/Receiver.xml")
        End If

  If CheckForItem1.Contains(Me.Textbox2.Text.ToString) Then
     
   Msgbox" The data already exist"
   exit sub
   
    ' However need help with modifying the following code:
        Dim xNew1 As XElement = New XElement("Receiver")
            xNew1.Add(New XElement("ReceiverID", (Existing ID of matching record---- (.<ReceiverID>.Value))****Does not work
            xNew1.Add(New XElement("Name", Me.Textbox2.Text.ToString))
            MyDocReceiver.Root.Add(xNew1)
            MyDocReceiver.Save(Application.StartupPath & "/Receiver.xml")
        End If


Thanks,

Victor
0
vcharles
Asked:
vcharles
  • 10
  • 5
1 Solution
 
vcharlesAuthor Commented:
Hello,

This is a better example of what I'm trying to do: I need help on the following line to add the ReceiverID that was found for the duplicate record:

xNewLink.Add(New XElement("ReceiverID", (el.<ReceiverID>.Value)))'**** Error Line

Code:

 'Receiver()
        Dim MyDocReceiver As XDocument = XDocument.Load(Application.StartupPath & "/Receiver.xml")
        Dim LargestReceiverID = (From el In MyDocReceiver.Descendants("Receiver") Select CInt(el.<ReceiverID>.Value)).ToList
        LargestReceiverID.Sort()
        Dim CheckForItem1 = (From el In MyDocReceiver.Descendants("Receiver").Elements("Name") Select el.Value).ToList
        If Not CheckForItem1.Contains(Me.Textbox2.Text.ToString) Then
            Dim xNew1 As XElement = New XElement("Receiver")
            xNew1.Add(New XElement("ReceiverID", (LargestReceiverID.Last() + 1)))
            xNew1.Add(New XElement("Name", Me.Textbox2.Text.ToString))
            MyDocReceiver.Root.Add(xNew1)
            MyDocReceiver.Save(Application.StartupPath & "/Receiver.xml")
        End If

  If CheckForItem1.Contains(Me.Textbox2.Text.ToString) Then
   Add already existed ReceiverID to Link.xml file.  
   
'Link
        Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml")
        Dim LinkID = (From el In MyDocLink.Descendants("Link") Select CInt(el.<LinkID>.Value)).ToList
        LinkID.Sort()
        Dim xNewLink As XElement = New XElement("Link")
        xNewLink.Add(New XElement("LinkID", (LinkID.Last() + 1)))
        xNewLink.Add(New XElement("ReceiverID", (el.<ReceiverID>.Value)))'**** Error Line
        MyDocLink.Root.Add(xNewLink)
        MyDocLink.Save(Application.StartupPath & "/Link.xml")

Thanks,

Victor
0
 
vcharlesAuthor Commented:
Hello,

Still can't figure out a solution, I need to somehow save the value of the ReceiverID when a duplicate is found. I believe it would be in that part of the code, but can't figure out the proper syntax.
 Dim x as integer
  If CheckForItem1.Contains(Me.Textbox2.Text.ToString) Then
   'Set x equal to ReceiverID of element with duplicate Receiver.  
   x = ReceiverID.
   
'Link
        Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml")
        Dim LinkID = (From el In MyDocLink.Descendants("Link") Select CInt(el.<LinkID>.Value)).ToList
        LinkID.Sort()
        Dim xNewLink As XElement = New XElement("Link")
        xNewLink.Add(New XElement("LinkID", (LinkID.Last() + 1)))
        xNewLink.Add(New XElement("ReceiverID", (x)'**** Include x in link file
        MyDocLink.Root.Add(xNewLink)
        MyDocLink.Save(Application.StartupPath & "/Link.xml")

0
 
Sudhakar PulivarthiCommented:
Hi vcharles,
Can u post the sample data.. along with expected output.

Sorry for the late reply.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
vcharlesAuthor Commented:
Hello,

Below is an example of the sample data and desired output.

Data source:

Receiver.xml:
ReceiverID = 1
Receiver = BEL
ReceiverID = 2
Receiver = CAN
ReceiverID = 3
Receiver = NLD
ReceiverID = 4
Receiver = USA

Donor.xml:
DonorID = 1
Donor = USA
DonorID = 2
Donor = ESP
DonorID = 3
Donor = CAN
DonorID = 4
Donor = DEU
DonorID = 5
Donor = NLD

Link.xml
ReceiverID = 1
DonorID = 2
ReceiverID = 1
DonorID = 1
ReceiverID = 2
DonorID = 3
ReceiverID = 3
DonorID = 4
ReceiverID = 1
DonorID = 5

A combobox displays all the receivers, and when the user selects BEL:

Desired Output:

In two Comboboxes (Receiver and Donor)

Receiver: BEL
Donor: CAN

Next  Previous (Buttons)

When User clicks on [Next] he/she sees:

Receiver: BEL
Donor: USA

When User clicks on [Next] aagain he/she sees:

Receiver: BEL
Donor: NLD

 The data displayed in the comboboxes depends on the IDs listed in link.xml and is filter by country selected in the combobox ( i.e. ReceiverID = 1  for BEL)

Thanks,

Victor
0
 
vcharlesAuthor Commented:
Please note the data sent in my previous post is actually in the proper xml format, but it was sent in the wrong format just as an example of the data it contains.
0
 
Sudhakar PulivarthiCommented:

I am not able to get the relationship expected in Link.xml from the Receiver and Donor.
Please explain the logic in its construction.
0
 
vcharlesAuthor Commented:
The link file holds the ID values of the Receiver and Donor files in order to avoid duplicate Receiver and Donor entries. When a user selects a Receiver and donor from a grid/dropdownlist and presses the save button, their IDs are saved in the link file. To display the data saved the IDs in the link file are used to display the combinations of Donors and receivers already saved.

Donor:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Donor>
    <DonorID>1</DonorID>
    <Name>BEL</Name>
  </Donor>
  <Donor>
    <DonorID>2</DonorID>
    <Name>USA</Name>
  </Donor>
  <Donor>
    <DonorID>3</DonorID>
    <Name>CAN</Name>
  </Donor>
  <Donor>
    <DonorID>4</DonorID>
    <Name>DEU</Name>
  </Donor>
 </Root>

Receiver:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Receiver>
    <ReceiverID>1</ReceiverID>
    <Name>BEL</Name>
  </Receiver>
  <Receiver>
    <ReceiverID>2</ReceiverID>
    <Name>USA</Name>
  </Receiver>
  <Receiver>
    <ReceiverID>3</ReceiverID>
    <Name>CAN</Name>
  </Receiver>
  <Receiver>
    <ReceiverID>4</ReceiverID>
    <Name>DEU</Name>
  </Receiver>
</Root>

Link:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Link>
    <LinkID>1</LinkID>
    <ReceiverID>3</ReceiverID>
    <DonorID>2</DonorID>
  </Link>
  <Link>
    <LinkID>1</LinkID>
    <ReceiverID>1</ReceiverID>
    <DonorID>3</DonorID>
  </Link>
  <Link>
    <LinkID>1</LinkID>
    <ReceiverID>2</ReceiverID>
    <DonorID>1</DonorID>
  </Link>
</Root>

My issue is, for example, when the users insert a country in a textbox for Donor, the code checks to see if the data is already available in Donor.xml,  if it does (A), I only want to pass the existing DonoID for the matching record to the link file and if it does not(B), I want to save it to the Donor.xml while creating a new ID and also save the ID created to the link.xml file. I am able to do the part(B), but can't figure out the first part(A). How do i identify the DonorD when there is a match in Part 1? Once I do, I can pass it the to link.xml file in Part 2 of my code:

Part 1:

 Dim x as integer
  If CheckForItem1.Contains(Me.Textbox2.Text.ToString) Then
   'Set x equal to ReceiverID of element with duplicate Receiver.  
   x = ReceiverID.
   
Part 2:

'Link
        Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml")
        Dim LinkID = (From el In MyDocLink.Descendants("Link") Select CInt(el.<LinkID>.Value)).ToList
        LinkID.Sort()
        Dim xNewLink As XElement = New XElement("Link")
        xNewLink.Add(New XElement("LinkID", (LinkID.Last() + 1)))
        xNewLink.Add(New XElement("ReceiverID", (x)'**** Include x in link file
        MyDocLink.Root.Add(xNewLink)
        MyDocLink.Save(Application.StartupPath & "/Link.xml")

Thanks,

Victor


0
 
Sudhakar PulivarthiCommented:
Hi,

These code sample will check in the donor xml to find out whether country Name is already present in the xml and gets the id of it

Dim search As String = "USA"
Dim searchId As String = Nothing
Dim dom As New XmlDocument()
dom.Load("C:\c.xml")

Dim list As XmlNodeList = dom.SelectNodes("//Root/Donor")

For Each node As XmlNode In list
      If node("Name").InnerText.Equals(search) Then
            searchId = node("DonorID").InnerText
            Exit For
      End If
Next

If Not String.IsNullOrEmpty(searchId) Then
' Country is already present in the xml, found the id

End If

0
 
vcharlesAuthor Commented:
Thank you, I don't understand that part of your code:

If Not String.IsNullOrEmpty(searchId) Then
' Country is already present in the xml, found the id

End If

Also, can I modify my code as follows:

Dim search As String = Textbox2.Text
Dim searchId As String = Nothing
Dim dom As New XmlDocument()
dom.Load("C:\Receiver.xml")
Dim list As XmlNodeList = dom.SelectNodes("//Root/Receiver")
 If CheckForItem1.Contains(Me.Textbox2.Text.ToString) Then
 .For Each node As XmlNode In list
      If node("Name").InnerText.Equals(search) Then
            searchId = node("ReceiverrID").InnerText
            Exit For
      End If
Next

End If
   
Part 2:

'Link
        Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml")
        Dim LinkID = (From el In MyDocLink.Descendants("Link") Select CInt(el.<LinkID>.Value)).ToList
        LinkID.Sort()
        Dim xNewLink As XElement = New XElement("Link")
        xNewLink.Add(New XElement("LinkID", (LinkID.Last() + 1)))
        xNewLink.Add(New XElement("ReceiverID", searchId))
        MyDocLink.Root.Add(xNewLink)
        MyDocLink.Save(Application.StartupPath & "/Link.xml")

0
 
Sudhakar PulivarthiCommented:
Hi

searchId check after the loop will tell whether the search text was found in the nodes i.e the country name as "USA" is found in the xml that search can from anywhere like from text box. Set the text which u want to search (duplicate), if found in existing xml then u can perform accordingly
>>
My issue is, for example, when the users insert a country in a textbox for Donor, the code checks to see if the data is already available in Donor.xml,  if it does (A), I only want to pass the existing DonorID for the matching record to the link file.
This existing donorId is what is searchId, when the searchId is empty then it was not found, else duplicate found and donor id is obtained to pass to the link file

0
 
vcharlesAuthor Commented:
Hi,

I think I'm getting there, but still running into some issues, CodeA is what I'm using, problem is it does work with your CodeB because of error on line:   Dim list As XmlNodeList = MyReceiver.SelectNodes("//Root/Receiver").  Error: "Selected Nodes is not a member of System.Xml.Linq.XDocument"

CodeA:
 Dim MyReceiverAs XDocument = XDocument.Load(Server.MapPath("~/App_Data/Receiver.xml"))
        Dim LargestReceiverID = (From el In MyReceiver.Descendants("ReceiverTable") Select CInt(el.<Receiver_ID>.Value)).ToList
        LargestReceiverID.Sort()

        Dim CheckForItemC = (From el In MyReceiver.Descendants("ReceiverTable").Elements("Receiver") Select el.Value).ToList
        If Not CheckForItemC.Contains(Me.TextBox2.Text.ToString) Then
            Dim xNewA As XElement = New XElement("ReceiverTable")
            xNewA.Add(New XElement("Receiver_ID", (LargestReceiverID.Last() + 1)))
            xNewA.Add(New XElement("Receiver", Me.TextBox2.Text.ToString))
            MyReceiver.Root.Add(xNewA)
            MyNSN.Save(Server.MapPath("~/App_Data/Receiver.xml"))
        End If

CodeB: Duplicates

        Dim searchId As String = Nothing
        ' Dim dom As New XmlDocument() 'Already declared
        'dom.Load("C:\Receiver.xml")
        Dim list As XmlNodeList = MyReceiver.SelectNodes("//Root/Receiver")  ****Error
        If CheckForItemC.Contains(Me.TextBox2.Text.ToString) Then  'Duplicates
            For Each node As XmlNode In list
                If node("Name").InnerText.Equals(search) Then
                    searchId = node("ReceiverID").InnerText
                    Exit For
                End If
            Next
            MsgBox(searchId)
        End If

searcid is then passed to Link.xml with CodeC

CodeC:

'Link
        Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml")
        Dim LinkID = (From el In MyDocLink.Descendants("Link") Select CInt(el.<LinkID>.Value)).ToList
        LinkID.Sort()
        Dim xNewLink As XElement = New XElement("Link")
        xNewLink.Add(New XElement("LinkID", (LinkID.Last() + 1)))
      If CheckForItemC.Contains(Me.TextBox2.Text.ToString)  then
        xNewLink.Add(New XElement("ReceiverID", searchId))
      Endif
     If Not CheckForItemC.Contains(Me.TextBox2.Text.ToString) Then
       xNewLink.Add(New XElement("ReceiverID", (LargestReceiverID.Last() + 1)))
     End If
        MyDocLink.Root.Add(xNewLink)
        MyDocLink.Save(Application.StartupPath & "/Link.xml")

0
 
vcharlesAuthor Commented:
I tried the following code, but eventhougfh duplicate is found, searchId is returned blank with the messagebox listed at the end of the code..

Dim search As String = TextBox2.Text
        Dim searchId As String = Nothing
        Dim dom As New XmlDocument()
        dom.Load(Server.MapPath("~/App_Data/Receiver.xml"))
        Dim list As XmlNodeList = dom.SelectNodes("//Root/Receiverr")

        If CheckForItem.Contains(Me.TextBox2.Text.ToString) Then
                  For Each node As XmlNode In list
                If node("Receiver").InnerText.Equals(search) Then
                    searchId = node("Receiver_ID").InnerText
                    Exit For
                End If
            Next
            MsgBox(searchId)
        End If
0
 
vcharlesAuthor Commented:
Hi,

I modified my code to match the latest Receiver.xml format below but I receiveD the following

 error: Object reference not set to an instance of an object.

 on line:: If node("Receiver").InnerText.Equals(search) Then


<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <ReceiverTable>
    <Receiver_ID>1</Receiver_ID>
    <Receiver>USA</Receiver>
  </ReceiverTable>
  </Root>

Code modified with "ReceiverTable":
Dim search As String = TextBox2.Text
        Dim searchId As String = Nothing
        Dim dom As New XmlDocument()
        dom.Load(Server.MapPath("~/App_Data/Receiver.xml"))
        Dim list As XmlNodeList = dom.SelectNodes("//ReceiverTable/Receiverr") ***Modified

        If CheckForItem.Contains(Me.TextBox2.Text.ToString) Then
                  For Each node As XmlNode In list
                If node("Receiver").InnerText.Equals(search) Then
                    searchId = node("Receiver_ID").InnerText
                    Exit For
                End If
            Next
            MsgBox(searchId)
        End If
0
 
Sudhakar PulivarthiCommented:
Hi,

The xml node list should select ReceiverTable nodes not the Receiver nodes.
Dim list As XmlNodeList = dom.SelectNodes("//ReceiverTable")
0
 
vcharlesAuthor Commented:
Ii worked. Thanks!!!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now