Help with saving data to an xml file?


I'm using code A to filter a LinkTemp.xml file ans displayimng the data to two comboboxes. I'm than using Code B to Transfer the current record of the filtered data to a Linbk.xml file, the problem is in CodeB:

on line: MyLink.Element("Root").Add(GetNodeForID.First)

when I enter 9 in the Textbox to search for ReceiverID = 9 in LinkTemp.xml all is ok (Code A) then When I press the Transfer button to activate Code B to transfer the results to Link.xml,  I receive the following error: Sequence contains no elements. I don't get this error when I enter other values in the Textbox.

Any ideas what is causing this error?



Code A:
 If IsPostBack = True Then
            ' create a new XML Document object for each XML file
            Dim xdDonor As New Xml.XmlDocument
            Dim xdLink As New Xml.XmlDocument
            Dim xdReceiver As New Xml.XmlDocument

            ' load all of the XML documents

            ' This outer loop will iterate over the Link file where the ReceiverID node is equal to our search criteria
            For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/Link[ReceiverID='" & TextBox2.Text & "']")

                ' Extract the ReceiverID and DonorID values from the current node
                Dim ReceiverID As String
                Dim DonorID As String

                ReceiverID = xnLink.SelectSingleNode("ReceiverID").InnerText
                DonorID = xnLink.SelectSingleNode("DonorID").InnerText

                ' Perform another similar search based upon the Donor XML file
                For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root/Donor[DonorID='" & DonorID & "']")

                    ' Extract the DonorID and Name values from the current node
                    Dim DonorID2 As String
                    Dim Name As String

                    DonorID2 = xnDonor.SelectSingleNode("DonorID").InnerText
                    Name = xnDonor.SelectSingleNode("Name").InnerText
                    dtDonor.Rows.Add({DonorID2, Name})

                ' Perform yet another practically identical search on the Receiver XML file
                For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/Root/Receiver[ReceiverID='" & ReceiverID & "']")
                    ' Extract the ReceiverID and Name values from the current node
                    Dim ReceiverID2 As String
                    Dim Name As String

                    ReceiverID2 = xnReceiver.SelectSingleNode("ReceiverID").InnerText
                    Name = xnReceiver.SelectSingleNode("Name").InnerText
                    dtReceiver.Rows.Add({ReceiverID2, Name})

        End If

        CmbDonor.DataSource = dtDonor
        CmbDonor.DataTextField = "Name"
        CmbDonor.DataValueField = "ID"

        CmbRec.DataSource = dtReceiver
        CmbRec.DataTextField = "Name"
        CmbRec.DataValueField = "ID"

Code B:

Dim MyLink As XDocument = XDocument.Load(Server.MapPath("~/App_Data/Link.xml"))
        Dim XT As XElement = XElement.Load(Server.MapPath("~/App_Data/LinkTemp.xml"))
        Dim GetNodeForID = From c In XT.Elements("Link")
                      Where (c.Element("Link_ID").Value = TextBox2.Text)
                      Select New XElement("Link",
                                          New XElement("Link_ID", TextBox2.Text),
                                          New XElement("ReceiverID", c.Element("ReceiverID").Value),
                                          New XElement("DonorID", c.Element("DonorID").Value))
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
At code B you need to check if your sequence contains results.
If (Not IsNothing(GetNodeForID)) And (Not GetNodeForID.Any()) Then

Check: ( In C#, but basically it checks if the linq result has valid entries)
Miguel OzSoftware EngineerCommented:
Sorry small typo, code should be:
If (Not IsNothing(GetNodeForID)) And (GetNodeForID.Any()) Then

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank You!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.