Link to home
Create AccountLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with saving data to an xml file?

Hello,

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?

Thanks,

Victor

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
            xdDonor.Load(Server.MapPath("~/App_Data/Donor.xml"))
            xdLink.Load(Server.MapPath("~/App_Data/LinkTemp.xml"))
            xdReceiver.Load(Server.MapPath("~/App_Data/Receiver.xml"))

            ' 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})
                Next

                ' 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})

                Next
            Next
        End If

        CmbDonor.DataSource = dtDonor
        CmbDonor.DataTextField = "Name"
        CmbDonor.DataValueField = "ID"
        CmbDonor.DataBind()

        CmbRec.DataSource = dtReceiver
        CmbRec.DataTextField = "Name"
        CmbRec.DataValueField = "ID"
        CmbRec.DataBind()

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))
        MyLink.Element("Root").Add(GetNodeForID.First)
        MyLink.Save((Server.MapPath("~/App_Data/Link.xml")))
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

At code B you need to check if your sequence contains results.
REplace:
      MyLink.Element("Root").Add(GetNodeForID.First)
        MyLink.Save((Server.MapPath("~/App_Data/Link.xml")))
With:
If (Not IsNothing(GetNodeForID)) And (Not GetNodeForID.Any()) Then
      MyLink.Element("Root").Add(GetNodeForID.First)
        MyLink.Save((Server.MapPath("~/App_Data/Link.xml")))
EndIf

Check: ( In C#, but basically it checks if the linq result has valid entries)
http://stackoverflow.com/questions/41319/checking-if-a-list-is-empty-with-linq
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Victor  Charles

ASKER

Thank You!