Avatar of Victor  Charles
Victor Charles
Flag 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")))
ASP.NETVisual Basic.NETXML

Avatar of undefined
Last Comment
Victor Charles

8/22/2022 - Mon