[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with saving data to an xml file?

Posted on 2011-10-07
3
Medium Priority
?
336 Views
Last Modified: 2012-05-12
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")))
0
Comment
Question by:vcharles
  • 2
3 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 36934570
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
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 2000 total points
ID: 36934599
Sorry small typo, code should be:
If (Not IsNothing(GetNodeForID)) And (GetNodeForID.Any()) Then
      MyLink.Element("Root").Add(GetNodeForID.First)
        MyLink.Save((Server.MapPath("~/App_Data/Link.xml")))
EndIf
0
 

Author Closing Comment

by:vcharles
ID: 36939347
Thank You!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

873 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