Solved

Help with saving duplicate data found to an xml file

Posted on 2011-09-24
18
514 Views
Last Modified: 2012-05-12
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
Comment
Question by:vcharles
  • 10
  • 5
18 Comments
 

Author Comment

by:vcharles
ID: 36594122
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
 

Author Comment

by:vcharles
ID: 36595511
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
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 36714356
Hi vcharles,
Can u post the sample data.. along with expected output.

Sorry for the late reply.
0
 

Author Comment

by:vcharles
ID: 36715840
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
 

Author Comment

by:vcharles
ID: 36715851
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
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 36804882

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
 

Author Comment

by:vcharles
ID: 36812808
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 500 total points
ID: 36812862
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
 

Author Comment

by:vcharles
ID: 36813149
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
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 36814182
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
 

Author Comment

by:vcharles
ID: 36814691
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
 

Author Comment

by:vcharles
ID: 36814925
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
 

Author Comment

by:vcharles
ID: 36815050
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
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 36815126
Hi,

The xml node list should select ReceiverTable nodes not the Receiver nodes.
Dim list As XmlNodeList = dom.SelectNodes("//ReceiverTable")
0
 

Author Comment

by:vcharles
ID: 36815163
Ii worked. Thanks!!!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now