Victor Charles
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 (GetNodeFo rID.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.MapPat h("~/App_D ata/Donor. xml"))
xdLink.Load(Server.MapPath ("~/App_Da ta/LinkTem p.xml"))
xdReceiver.Load(Server.Map Path("~/Ap p_Data/Rec eiver.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[Recei verID='" & TextBox2.Text & "']")
' Extract the ReceiverID and DonorID values from the current node
Dim ReceiverID As String
Dim DonorID As String
ReceiverID = xnLink.SelectSingleNode("R eceiverID" ).InnerTex t
DonorID = xnLink.SelectSingleNode("D onorID").I nnerText
' Perform another similar search based upon the Donor XML file
For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root /Donor[Don orID='" & 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").Inn erText
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("/R oot/Receiv er[Receive rID='" & ReceiverID & "']")
' Extract the ReceiverID and Name values from the current node
Dim ReceiverID2 As String
Dim Name As String
ReceiverID2 = xnReceiver.SelectSingleNod e("Receive rID").Inne rText
Name = xnReceiver.SelectSingleNod e("Name"). InnerText
dtReceiver.Rows.Add({Recei verID2, 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.MapP ath("~/App _Data/Link .xml"))
Dim XT As XElement = XElement.Load(Server.MapPa th("~/App_ Data/LinkT emp.xml"))
Dim GetNodeForID = From c In XT.Elements("Link")
Where (c.Element("Link_ID").Valu e = TextBox2.Text)
Select New XElement("Link",
New XElement("Link_ID", TextBox2.Text),
New XElement("ReceiverID", c.Element("ReceiverID").Va lue),
New XElement("DonorID", c.Element("DonorID").Value ))
MyLink.Element("Root").Add (GetNodeFo rID.First)
MyLink.Save((Server.MapPat h("~/App_D ata/Link.x ml")))
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
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.MapPat
xdLink.Load(Server.MapPath
xdReceiver.Load(Server.Map
' 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/
' Extract the ReceiverID and DonorID values from the current node
Dim ReceiverID As String
Dim DonorID As String
ReceiverID = xnLink.SelectSingleNode("R
DonorID = xnLink.SelectSingleNode("D
' Perform another similar search based upon the Donor XML file
For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root
' Extract the DonorID and Name values from the current node
Dim DonorID2 As String
Dim Name As String
DonorID2 = xnDonor.SelectSingleNode("
Name = xnDonor.SelectSingleNode("
dtDonor.Rows.Add({DonorID2
Next
' Perform yet another practically identical search on the Receiver XML file
For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/R
' Extract the ReceiverID and Name values from the current node
Dim ReceiverID2 As String
Dim Name As String
ReceiverID2 = xnReceiver.SelectSingleNod
Name = xnReceiver.SelectSingleNod
dtReceiver.Rows.Add({Recei
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.MapP
Dim XT As XElement = XElement.Load(Server.MapPa
Dim GetNodeForID = From c In XT.Elements("Link")
Where (c.Element("Link_ID").Valu
Select New XElement("Link",
New XElement("Link_ID", TextBox2.Text),
New XElement("ReceiverID", c.Element("ReceiverID").Va
New XElement("DonorID", c.Element("DonorID").Value
MyLink.Element("Root").Add
MyLink.Save((Server.MapPat
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank You!
REplace:
MyLink.Element("Root").Add
MyLink.Save((Server.MapPat
With:
If (Not IsNothing(GetNodeForID)) And (Not GetNodeForID.Any()) Then
MyLink.Element("Root").Add
MyLink.Save((Server.MapPat
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