Victor Charles
asked on
Help with If statement to filter data in Link.xml file
Hello,
I'm trying to display data in two combo boxes based on values entered in a textbox. The trick is donor.xm; and Receiver.xml are linked to Link.xml by ReceiverID and DonorID. Below is an example of all three xml files.
For example, when I seach for ReceiverID = 3 in my Textbox, CmbRec should have value: CCCR
and if multiple records are found, I need to move to the next record using the Next/Previous buttons.
However I'm not getting the correct value, I'm getting a value of 1
Can you please take a look at my If statement to see what I'm doing wrong..
Thanks,
Victor
Link.xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Link>
<ReceiverID>1</ReceiverID>
<DonorID>2</DonorID>
</Link>
<Link>
<ReceiverID>3</ReceiverID>
<DonorID>3</DonorID>
</Link>
</Root>
Receiver.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Receiver>
<ReceiverID>1</ReceiverID>
<Name>AAAA</Name>
</Receiver>
<Receiver>
<ReceiverID>2</ReceiverID>
<Name>BBBB</Name>
</Receiver>
<Receiver>
<ReceiverID>3</ReceiverID>
<Name>CCCR</Name>
</Receiver>
</Root>
Donor.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Donor>
<DonorID>1</DonorID>
<Name>AAA</Name>
</Donor>
<Donor>
<DonorID>2</DonorID>
<Name>BBBB</Name>
</Donor>
<Donor>
<DonorID>3</DonorID>
<Name>CCCD</Name>
</Donor>
</Root>
Code:
DonorStream.Load(Server.Ma pPath("~/A pp_Data/Do nor.xml")) 'My.Resources.Donor)
RecStream.Load(Server.MapP ath("~/App _Data/Rece iver.xml") ) 'My.Resources.Receiver)
LinkStream.Load(Server.Map Path("~/Ap p_Data/Lin k.xml")) 'My.Resources.Link)
Dim lnk As XmlNodeList = LinkStream.SelectNodes("Ro ot/Link")
Dim RecName As XmlNodeList = RecStream.SelectNodes("Roo t/Receiver ")
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R oot/Donor" )
'Create Table
Dim donorlst As New DataTable()
donorlst.Columns.Add("ID")
donorlst.Columns.Add("Name ")
Dim reclst As New DataTable
reclst.Columns.Add("ID")
reclst.Columns.Add("Name")
'Load Link.xml
Dim MyLink As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/Link .xml"))
'Create varaible CheckForItem to see if I being searched exit in Link.xml
Dim CheckForItem = (From el In MyLink.Descendants("Link") .Elements( "ReceiverI D") Select el.Value).ToList
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In lnk
If CheckForItem.Contains(Me.T extBox2.Te xt.ToStrin g) Then
Dim rID As Integer = Val(n.ChildNodes(0).InnerT ext)
Dim dID As Integer = Val(n.ChildNodes(1).InnerT ext)
'Loop Through all records in Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe xt = rID.ToString Then
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerTe xt})
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe xt = dID.ToString Then
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT ext, node.ChildNodes(1).InnerTe xt})
'donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe xt})
End If
Next
End If
Next
CmbDonor.DataSource = donorlst
CmbDonor.DataTextField = "Name"
CmbDonor.DataValueField = "ID"
CmbDonor.DataBind()
CmbRec.DataSource = reclst
CmbRec.DataTextField = "Name"
CmbRec.DataValueField = "ID"
CmbRec.DataBind()
I'm trying to display data in two combo boxes based on values entered in a textbox. The trick is donor.xm; and Receiver.xml are linked to Link.xml by ReceiverID and DonorID. Below is an example of all three xml files.
For example, when I seach for ReceiverID = 3 in my Textbox, CmbRec should have value: CCCR
and if multiple records are found, I need to move to the next record using the Next/Previous buttons.
However I'm not getting the correct value, I'm getting a value of 1
Can you please take a look at my If statement to see what I'm doing wrong..
Thanks,
Victor
Link.xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Link>
<ReceiverID>1</ReceiverID>
<DonorID>2</DonorID>
</Link>
<Link>
<ReceiverID>3</ReceiverID>
<DonorID>3</DonorID>
</Link>
</Root>
Receiver.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Receiver>
<ReceiverID>1</ReceiverID>
<Name>AAAA</Name>
</Receiver>
<Receiver>
<ReceiverID>2</ReceiverID>
<Name>BBBB</Name>
</Receiver>
<Receiver>
<ReceiverID>3</ReceiverID>
<Name>CCCR</Name>
</Receiver>
</Root>
Donor.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Donor>
<DonorID>1</DonorID>
<Name>AAA</Name>
</Donor>
<Donor>
<DonorID>2</DonorID>
<Name>BBBB</Name>
</Donor>
<Donor>
<DonorID>3</DonorID>
<Name>CCCD</Name>
</Donor>
</Root>
Code:
DonorStream.Load(Server.Ma
RecStream.Load(Server.MapP
LinkStream.Load(Server.Map
Dim lnk As XmlNodeList = LinkStream.SelectNodes("Ro
Dim RecName As XmlNodeList = RecStream.SelectNodes("Roo
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R
'Create Table
Dim donorlst As New DataTable()
donorlst.Columns.Add("ID")
donorlst.Columns.Add("Name
Dim reclst As New DataTable
reclst.Columns.Add("ID")
reclst.Columns.Add("Name")
'Load Link.xml
Dim MyLink As XDocument = XDocument.Load(Server.MapP
'Create varaible CheckForItem to see if I being searched exit in Link.xml
Dim CheckForItem = (From el In MyLink.Descendants("Link")
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In lnk
If CheckForItem.Contains(Me.T
Dim rID As Integer = Val(n.ChildNodes(0).InnerT
Dim dID As Integer = Val(n.ChildNodes(1).InnerT
'Loop Through all records in Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerTe
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT
'donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe
End If
Next
End If
Next
CmbDonor.DataSource = donorlst
CmbDonor.DataTextField = "Name"
CmbDonor.DataValueField = "ID"
CmbDonor.DataBind()
CmbRec.DataSource = reclst
CmbRec.DataTextField = "Name"
CmbRec.DataValueField = "ID"
CmbRec.DataBind()
ASKER
Can I use both XPath and Linq in thje same project? I looked at your link but still can not figure out how to get it to work withe two xml files linked to my link.xml file, can you please help me with the code.
Thanks,
victor
Thanks,
victor
ASKER
By the way, I only receive records when I enter a ReceiverID that exit in Link.xml, if I enter the wrong ID no records are returned, so it seems to me that the application understands " If CheckForItem.Contains(Me.T extBox2.Te xt.ToStrin g)" If you want I can send you the project.
Sure, you can use both... I was just trying to simply the problem for you...
If you're more comfortable using LINQ rather than XPath, that's OK. But you'll have to tweak that LINQ query to only look for the search item in the current "link" node (not all of the "link" nodes).
If you're more comfortable using LINQ rather than XPath, that's OK. But you'll have to tweak that LINQ query to only look for the search item in the current "link" node (not all of the "link" nodes).
ASKER
Can you please send me a sample code on how you would achieve the same using XPATH.
Thanks.
Thanks.
OK... here is what I'm talking about. Note: This was typed by hand, so watch for typos...
This XPath query will find all "link" nodes that that have an RecieverID equal to your search critera. You could use this same technique to speed up the other searches as well, since an XPath query is almost always better than manualy looping through the XML hunting for matching items (that's what XPath is for!).
Yeah, I know... XPath is a strange beast, but it's worth learning. There are a gazillion resources on the web to help speed you along.
This XPath query will find all "link" nodes that that have an RecieverID equal to your search critera. You could use this same technique to speed up the other searches as well, since an XPath query is almost always better than manualy looping through the XML hunting for matching items (that's what XPath is for!).
Yeah, I know... XPath is a strange beast, but it's worth learning. There are a gazillion resources on the web to help speed you along.
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In MyLink.SelectNodes("/root/link[RecieverID=" & Me.TextBox2.Text & "]")
Dim rID As Integer = Val(n.ChildNodes(0).InnerText)
Dim dID As Integer = Val(n.ChildNodes(1).InnerText)
'Loop Through all records in Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerText = rID.ToString Then
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerText})
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerText = dID.ToString Then
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerText, node.ChildNodes(1).InnerText})
'donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerText})
End If
Next
Next
ASKER
Thanks a million. I will try it and get back to you.
ASKER
Hi,
I tried your code, but I'm getting error:
SelectNodes is not a member of System.Xml.Linq.XDocument
On line:
For Each n As XmlNode In MyLink.SelectNodes("/root/ link[Recie verID=" & Me.TextBox2.Text & "]")
Code:
Protected Sub Button9_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button9.Click
DonorStream.Load(Server.Ma pPath("~/A pp_Data/Do nor.xml")) 'My.Resources.Donor)
RecStream.Load(Server.MapP ath("~/App _Data/Rece iver.xml") ) 'My.Resources.Receiver)
LinkStream.Load(Server.Map Path("~/Ap p_Data/Lin k.xml")) 'My.Resources.Link)
Dim lnk As XmlNodeList = LinkStream.SelectNodes("Ro ot/Link")
Dim RecName As XmlNodeList = RecStream.SelectNodes("Roo t/Receiver ")
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R oot/Donor" )
'Create Table
Dim donorlst As New DataTable()
donorlst.Columns.Add("ID")
donorlst.Columns.Add("Name ")
Dim reclst As New DataTable
reclst.Columns.Add("ID")
reclst.Columns.Add("Name")
'Load Link.xml
Dim MyLink As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/Link .xml"))
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In MyLink.SelectNodes("/root/ link[Recie verID=" & Me.TextBox2.Text & "]")
Dim rID As Integer = Val(n.ChildNodes(0).InnerT ext)
Dim dID As Integer = Val(n.ChildNodes(1).InnerT ext)
'Loop Through all records in Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe xt = rID.ToString Then
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerTe xt})
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe xt = dID.ToString Then
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT ext, node.ChildNodes(1).InnerTe xt})
'donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe xt})
End If
Next
Next
End Sub
I tried your code, but I'm getting error:
SelectNodes is not a member of System.Xml.Linq.XDocument
On line:
For Each n As XmlNode In MyLink.SelectNodes("/root/
Code:
Protected Sub Button9_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button9.Click
DonorStream.Load(Server.Ma
RecStream.Load(Server.MapP
LinkStream.Load(Server.Map
Dim lnk As XmlNodeList = LinkStream.SelectNodes("Ro
Dim RecName As XmlNodeList = RecStream.SelectNodes("Roo
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R
'Create Table
Dim donorlst As New DataTable()
donorlst.Columns.Add("ID")
donorlst.Columns.Add("Name
Dim reclst As New DataTable
reclst.Columns.Add("ID")
reclst.Columns.Add("Name")
'Load Link.xml
Dim MyLink As XDocument = XDocument.Load(Server.MapP
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In MyLink.SelectNodes("/root/
Dim rID As Integer = Val(n.ChildNodes(0).InnerT
Dim dID As Integer = Val(n.ChildNodes(1).InnerT
'Loop Through all records in Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerTe
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT
'donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe
End If
Next
Next
End Sub
Sorry, how about
LinkStream.SelectNodes
LinkStream.SelectNodes
ASKER
I imported Imports System.Xml.XPath and tried XPath in the code below but it's still not working.
'Load Link.xml
Dim MyLink As Xml.XPath.XPathDocument = XPathDocument.Load(Server. MapPath("~ /App_Data/ Link.xml") )
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In MyLink.SelectNodes("/root/ link[Recie verID=" & Me.TextBox2.Text & "]")
Dim rID As Integer = Val(n.ChildNodes(0).InnerT ext)
Dim dID As Integer = Val(n.ChildNodes(1).InnerT ext)
'Load Link.xml
Dim MyLink As Xml.XPath.XPathDocument = XPathDocument.Load(Server.
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In MyLink.SelectNodes("/root/
Dim rID As Integer = Val(n.ChildNodes(0).InnerT
Dim dID As Integer = Val(n.ChildNodes(1).InnerT
ASKER
Hello,
I modified the code but it still doesn't filter the Link file based on the value entered in the Textbox, can you please take a look at my code to see what I'm doing wrong.
Protected Sub Search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Search.Click
DonorStream.Load(Server.Ma pPath("~/A pp_Data/Do nor.xml")) 'My.Resources.Donor)
RecStream.Load(Server.MapP ath("~/App _Data/Rece iver.xml") ) 'My.Resources.Receiver)
LinkStream.Load(Server.Map Path("~/Ap p_Data/Lin k.xml")) 'My.Resources.Link)
Dim lnk As XmlNodeList = LinkStream.SelectNodes("Ro ot/Link")
Dim RecName As XmlNodeList = RecStream.SelectNodes("Roo t/Receiver ")
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R oot/Donor" )
'Create Table
Dim donorlst As New DataTable()
donorlst.Columns.Add("ID")
donorlst.Columns.Add("Name ")
Dim reclst As New DataTable
reclst.Columns.Add("ID")
reclst.Columns.Add("Name")
'Load Link.xml
Dim MyLink As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/Link .xml"))
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In LinkStream.SelectNodes("/r oot/link[R ecieverID= " & Me.TextBox2.Text & "]")
Dim rID As Integer = Val(n.ChildNodes(0).InnerT ext)
Dim dID As Integer = Val(n.ChildNodes(1).InnerT ext)
'Loop Through all records in Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe xt = rID.ToString Then
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerTe xt})
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe xt = dID.ToString Then
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT ext, node.ChildNodes(1).InnerTe xt})
'donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe xt})
End If
Next
Next
End Sub
Thanks,
Victor
I modified the code but it still doesn't filter the Link file based on the value entered in the Textbox, can you please take a look at my code to see what I'm doing wrong.
Protected Sub Search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Search.Click
DonorStream.Load(Server.Ma
RecStream.Load(Server.MapP
LinkStream.Load(Server.Map
Dim lnk As XmlNodeList = LinkStream.SelectNodes("Ro
Dim RecName As XmlNodeList = RecStream.SelectNodes("Roo
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R
'Create Table
Dim donorlst As New DataTable()
donorlst.Columns.Add("ID")
donorlst.Columns.Add("Name
Dim reclst As New DataTable
reclst.Columns.Add("ID")
reclst.Columns.Add("Name")
'Load Link.xml
Dim MyLink As XDocument = XDocument.Load(Server.MapP
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In LinkStream.SelectNodes("/r
Dim rID As Integer = Val(n.ChildNodes(0).InnerT
Dim dID As Integer = Val(n.ChildNodes(1).InnerT
'Loop Through all records in Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerTe
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT
'donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe
End If
Next
Next
End Sub
Thanks,
Victor
ASKER
Hello,
Below is the link to the project.
http://www.speedyshare.com/files/30631341/AmendProject.zip
For example when I enter 2 in my Textbox, The comboboxes should display the first record where ReceiverID = 2 in Link.xml and if more than 1 record exist they should be viewed by clicking on the Next Button.
Can you please take a look at the project to help me solve this issue..
Thanks
Victor
Below is the link to the project.
http://www.speedyshare.com/files/30631341/AmendProject.zip
For example when I enter 2 in my Textbox, The comboboxes should display the first record where ReceiverID = 2 in Link.xml and if more than 1 record exist they should be viewed by clicking on the Next Button.
Can you please take a look at the project to help me solve this issue..
Thanks
Victor
ASKER
Hello,
I'm trying to use Xpath but it's not working with some of existind variables (i.e Dim rID As Integer = Val(n.ChildNodes(0).InnerT ext)), can you please help me figure out how to integrate the Xpath command with my existing code: I hard coded the value of the Textbox on the code below, but it doesn't undertsand when I use Textbox2.text.
Dim iterator As System.Xml.XPath.XPathNode Iterator = nav.Select("//ReceiverID[. = Textbox2.Text]/parent::nod e()/Link")
Code:
Dim DonorStream As New System.Xml.XPath.XPathDocu ment(Serve r.MapPath( "~/App_Dat a/Donor.xm l"))
Dim RecStream As New System.Xml.XPath.XPathDocu ment(Serve r.MapPath( "~/App_Dat a/Receiver .xml"))
Dim LinkStream As New System.Xml.XPath.XPathDocu ment(Serve r.MapPath( "~/App_Dat a/Link.xml "))
''Create Table
Dim donorlst As New DataTable()
donorlst.Columns.Add("ID")
donorlst.Columns.Add("Name ")
Dim reclst As New DataTable
reclst.Columns.Add("ID")
reclst.Columns.Add("Name")
'' MsgBox(TextBox2.Text)
''Load Link.xml
' Dim MyLink As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/Link .xml"))
Dim nav As System.Xml.XPath.XPathNavi gator = LinkStream.CreateNavigator ()
''Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
Dim iterator As System.Xml.XPath.XPathNode Iterator = nav.Select("//ReceiverID[. = '1']/parent::node()/Link")
Do While iterator.MoveNext
' For Each n As XmlNode In LinkStream.SelectNodes("/r oot/link[R eceiverID= " & Me.TextBox2.Text & "]")
Dim rID As Integer = Val(n.ChildNodes(0).InnerT ext)
Dim dID As Integer = Val(n.ChildNodes(1).InnerT ext)
' 'Loop Through all records inn Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe xt = rID.ToString Then
reclst.Rows.Add(New Object() {rID, node.ChildNodes(0).InnerTe xt})
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe xt = dID.ToString Then
donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe xt})
''donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe xt})
End If
Next
Loop
End Sub
Thanks,
Victor
I'm trying to use Xpath but it's not working with some of existind variables (i.e Dim rID As Integer = Val(n.ChildNodes(0).InnerT
Dim iterator As System.Xml.XPath.XPathNode
Code:
Dim DonorStream As New System.Xml.XPath.XPathDocu
Dim RecStream As New System.Xml.XPath.XPathDocu
Dim LinkStream As New System.Xml.XPath.XPathDocu
''Create Table
Dim donorlst As New DataTable()
donorlst.Columns.Add("ID")
donorlst.Columns.Add("Name
Dim reclst As New DataTable
reclst.Columns.Add("ID")
reclst.Columns.Add("Name")
'' MsgBox(TextBox2.Text)
''Load Link.xml
' Dim MyLink As XDocument = XDocument.Load(Server.MapP
Dim nav As System.Xml.XPath.XPathNavi
''Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
Dim iterator As System.Xml.XPath.XPathNode
Do While iterator.MoveNext
' For Each n As XmlNode In LinkStream.SelectNodes("/r
Dim rID As Integer = Val(n.ChildNodes(0).InnerT
Dim dID As Integer = Val(n.ChildNodes(1).InnerT
' 'Loop Through all records inn Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe
reclst.Rows.Add(New Object() {rID, node.ChildNodes(0).InnerTe
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe
donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe
''donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe
End If
Next
Loop
End Sub
Thanks,
Victor
I'll work on it later today...
ASKER
I'm looking froward to hear back from you,
Thanks for your efforts.
Victor
Thanks for your efforts.
Victor
OK here is a complete and working example... with some comments and debug output so you can see what's going on.
It's not a complete "drop in" replacement, since I didn't setup a web page...
It's not a complete "drop in" replacement, since I didn't setup a web page...
' 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("Donor.xml")
xdLink.Load("Link.xml")
xdReceiver.Load("Receiver.xml")
' I'm faking the text box here...
Dim TextBoxText As String = "1"
' 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='" & TextBoxText & "']")
' 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
Debug.WriteLine("From Link.xml...")
Debug.WriteLine("ReceiverID=" & ReceiverID)
Debug.WriteLine("DonorID=" & DonorID)
' 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
Debug.WriteLine("From Donor.xml...")
Debug.WriteLine("DonorID=" & DonorID2)
Debug.WriteLine("Name=" & Name)
Next
' Perform yet another practically identical search on the Receiver XML file
For Each xnReceiver As Xml.XmlNode In xdDonor.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
Debug.WriteLine("From Reciever.xml...")
Debug.WriteLine("ReceiverID=" & ReceiverID2)
Debug.WriteLine("Name=" & Name)
Next
Next
See? XPath is not so bad... I use it along with LINQ. So, I'd encourage you to learn both techniques...
Yikes! I found a typo...
For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/R oot/Receiv er[Receive rID='" & ReceiverID & "']")
^^^^^^^
For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/R
^^^^^^^
ASKER
I don't know what's wrong, my comboboxes are blank and my message box statements are not executing, only first message box with HHHH appears, can you please send me the xml files you are using, perhaps we have different format, as I've been modifying them to try to solve the problem. Thanks.
Code:
Protected Sub Button10_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button10.Click
' create a new XML Document object for each XML file
If IsPostBack = True Then
MsgBox("HHHHHH")
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/Link.xm l"))
xdReceiver.Load(Server.Map Path("~/Ap p_Data/Rec eiver.xml" ))
' I'm faking the text box here...
Dim TextBoxText As String = "1"
' 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='" & TextBoxText & "']")
' 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
'Debug.WriteLine("From Link.xml...")
'Debug.WriteLine("Receiver ID=" & ReceiverID)
MsgBox("ReceiverID=" & ReceiverID)
MsgBox("DonorID=" & DonorID)
' 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
' Debug.WriteLine("From Donor.xml...")
MsgBox("DonorID=" & DonorID2)
MsgBox("Name=" & Name)
Next
' Perform yet another practically identical search on the Receiver XML file
'For Each xnReceiver As Xml.XmlNode In xdDonor.SelectNodes("/Root /Receiver[ ReceiverID ='" & ReceiverID & "']")
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
'Debug.WriteLine("From Reciever.xml...")
MsgBox("ReceiverID=" & ReceiverID2)
MsgBox("Name=" & Name)
Next
Next
End If
End Sub
Code:
Protected Sub Button10_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button10.Click
' create a new XML Document object for each XML file
If IsPostBack = True Then
MsgBox("HHHHHH")
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
' I'm faking the text box here...
Dim TextBoxText As String = "1"
' 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
'Debug.WriteLine("From Link.xml...")
'Debug.WriteLine("Receiver
MsgBox("ReceiverID=" & ReceiverID)
MsgBox("DonorID=" & DonorID)
' 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("
' Debug.WriteLine("From Donor.xml...")
MsgBox("DonorID=" & DonorID2)
MsgBox("Name=" & Name)
Next
' Perform yet another practically identical search on the Receiver XML file
'For Each xnReceiver As Xml.XmlNode In xdDonor.SelectNodes("/Root
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
'Debug.WriteLine("From Reciever.xml...")
MsgBox("ReceiverID=" & ReceiverID2)
MsgBox("Name=" & Name)
Next
Next
End If
End Sub
ASKER
SWhouldn't this be:
For Each xdLink As Xml.XmlNode In xdLink.SelectNodes("/Root/ Link[Recei verID='" & TextBoxText & "']")
instead of:
For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/ Link[Recei verID='" & TextBoxText & "']")
For Each xdLink As Xml.XmlNode In xdLink.SelectNodes("/Root/
instead of:
For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/
Well, the code works at my end... (er, well... after I fixed that typo with the "xdReceiver")
I can only assume that the XML files are not in the correct location and are not getting loaded?
I can only assume that the XML files are not in the correct location and are not getting loaded?
The XML files are the ones you posted at the top of this thread!
ASKER
Hi,
I think it's working, but the comboboxes are still blank, can you please include the code to populate the combo boxes with Receiver and Donor data after I filter the data based on the Textbox value.
Below is the code I initially used to populate the comboboxes when I did not filter the data.
Code:
DonorStream.Load(Server.Ma pPath("~/A pp_Data/Do nor.xml")) 'My.Resources.Donor)
RecStream.Load(Server.MapP ath("~/App _Data/Rece iver.xml") ) 'My.Resources.Receiver)
LinkStream.Load(Server.Map Path("~/Ap p_Data/Lin k.xml")) 'My.Resources.Link)
Dim lnk As XmlNodeList = LinkStream.SelectNodes("Ro ot/Link")
Dim RecName As XmlNodeList = RecStream.SelectNodes("Roo t/Receiver ")
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R oot/Donor" )
'Create Table
Dim donorlst As New DataTable()
donorlst.Columns.Add("ID")
donorlst.Columns.Add("Name ")
Dim reclst As New DataTable
reclst.Columns.Add("ID")
reclst.Columns.Add("Name")
'Load Link.xml
Dim MyLink As XDocument = XDocument.Load(Server.MapP ath("~/App _Data/Link .xml"))
'Create varaible CheckForItem to see if I being searched exit in Link.xml
Dim CheckForItem = (From el In MyLink.Descendants("Link") .Elements( "ReceiverI D") Select el.Value).ToList
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In lnk
If CheckForItem.Contains(Me.T extBox2.Te xt.ToStrin g) Then
Dim rID As Integer = Val(n.ChildNodes(0).InnerT ext)
Dim dID As Integer = Val(n.ChildNodes(1).InnerT ext)
'Loop Through all records in Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe xt = rID.ToString Then
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerTe xt})
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe xt = dID.ToString Then
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT ext, node.ChildNodes(1).InnerTe xt})
'donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe xt})
End If
Next
End If
Next
CmbDonor.DataSource = donorlst
CmbDonor.DataTextField = "Name"
CmbDonor.DataValueField = "ID"
CmbDonor.DataBind()
CmbRec.DataSource = reclst
CmbRec.DataTextField = "Name"
CmbRec.DataValueField = "ID"
CmbRec.DataBind()
Thanks
Victor
I think it's working, but the comboboxes are still blank, can you please include the code to populate the combo boxes with Receiver and Donor data after I filter the data based on the Textbox value.
Below is the code I initially used to populate the comboboxes when I did not filter the data.
Code:
DonorStream.Load(Server.Ma
RecStream.Load(Server.MapP
LinkStream.Load(Server.Map
Dim lnk As XmlNodeList = LinkStream.SelectNodes("Ro
Dim RecName As XmlNodeList = RecStream.SelectNodes("Roo
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R
'Create Table
Dim donorlst As New DataTable()
donorlst.Columns.Add("ID")
donorlst.Columns.Add("Name
Dim reclst As New DataTable
reclst.Columns.Add("ID")
reclst.Columns.Add("Name")
'Load Link.xml
Dim MyLink As XDocument = XDocument.Load(Server.MapP
'Create varaible CheckForItem to see if I being searched exit in Link.xml
Dim CheckForItem = (From el In MyLink.Descendants("Link")
'Loop Through all records in Link.xml and assign first childnode the rID and second childnode to dID
For Each n As XmlNode In lnk
If CheckForItem.Contains(Me.T
Dim rID As Integer = Val(n.ChildNodes(0).InnerT
Dim dID As Integer = Val(n.ChildNodes(1).InnerT
'Loop Through all records in Receiver.xml and if ReceiverID = Textbox2.text and if the firstnode
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerTe
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT
'donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerTe
End If
Next
End If
Next
CmbDonor.DataSource = donorlst
CmbDonor.DataTextField = "Name"
CmbDonor.DataValueField = "ID"
CmbDonor.DataBind()
CmbRec.DataSource = reclst
CmbRec.DataTextField = "Name"
CmbRec.DataValueField = "ID"
CmbRec.DataBind()
Thanks
Victor
What? You're back to your original design... with that LINQ statement that doesn't do what you are expecting it to do?
ASKER
No, I'm not back to the original design, I just need to fill in the comboboxes after I filter the data, the code you sent me doesn't include the displaying the data in the comboboxes, I'm not sure how to modify my old code to fill in those comboboxes.
ASKER
I tried to set the datasources to DonorID2 and ReceiverID2, but it does not work.
CmbDonor.DataSource = DonorID2
CmbDonor.DataTextField = "Name"
CmbDonor.DataValueField = "ID"
CmbDonor.DataBind()
CmbRec.DataSource = ReceiverID2
CmbRec.DataTextField = "Name"
CmbRec.DataValueField = "ID"
CmbRec.DataBind()
CmbDonor.DataSource = DonorID2
CmbDonor.DataTextField = "Name"
CmbDonor.DataValueField = "ID"
CmbDonor.DataBind()
CmbRec.DataSource = ReceiverID2
CmbRec.DataTextField = "Name"
CmbRec.DataValueField = "ID"
CmbRec.DataBind()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It worked Beatifully!!!
I'm trying to move to the next records with the code below for when mutiple matches are found but I'm getting error:
'CmbRec' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value
on Line: CmbRec.SelectedIndex = Val(Subnode1.InnerText) - 1
Code:
Dim NodeCount As Integer = LinkStream.SelectNodes("Ro ot/Link"). Count
If (NodeNumber < NodeCount) Then
NodeNumber += 1 'Move to some next node
Dim node As XmlNode = LinkStream.SelectSingleNod e("Root/Li nk[" & NodeNumber & "]")
Dim Subnode1 As XmlNode = node.ChildNodes(0)
Dim Subnode2 As XmlNode = node.ChildNodes(1)
CmbRec.SelectedIndex = Val(Subnode1.InnerText) - 1
CmbDonor.SelectedIndex = Val(Subnode2.InnerText) - 1
btnPrev.Enabled = True
If (NodeNumber = NodeCount) Then
btnNext.Enabled = False
End If
Else
NodeNumber = 0 ' Reset count so start searching from zero (You can change strategy)
' btnNext.Enabled = False
End If
I will sepend more time to work on MoveNext, MovePrevious, MoveFirst and MoveLast Features, will open a related case if I can't resolve it.
THANK YOU!!!
I'm trying to move to the next records with the code below for when mutiple matches are found but I'm getting error:
'CmbRec' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value
on Line: CmbRec.SelectedIndex = Val(Subnode1.InnerText) - 1
Code:
Dim NodeCount As Integer = LinkStream.SelectNodes("Ro
If (NodeNumber < NodeCount) Then
NodeNumber += 1 'Move to some next node
Dim node As XmlNode = LinkStream.SelectSingleNod
Dim Subnode1 As XmlNode = node.ChildNodes(0)
Dim Subnode2 As XmlNode = node.ChildNodes(1)
CmbRec.SelectedIndex = Val(Subnode1.InnerText) - 1
CmbDonor.SelectedIndex = Val(Subnode2.InnerText) - 1
btnPrev.Enabled = True
If (NodeNumber = NodeCount) Then
btnNext.Enabled = False
End If
Else
NodeNumber = 0 ' Reset count so start searching from zero (You can change strategy)
' btnNext.Enabled = False
End If
I will sepend more time to work on MoveNext, MovePrevious, MoveFirst and MoveLast Features, will open a related case if I can't resolve it.
THANK YOU!!!
ASKER
THANK YOU!!
If CheckForItem.Contains(Me.T
... this would always show true for every node even if it only matched one node.
I'd suggest that you abandon the LINQ technique and go back to good 'ole XPath query to find the nodes that match the item from the comboboxes
http://support.microsoft.com/kb/317069