?
Solved

Help with If statement to filter data in Link.xml file

Posted on 2011-10-04
29
Medium Priority
?
392 Views
Last Modified: 2012-05-12
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.MapPath("~/App_Data/Donor.xml")) 'My.Resources.Donor)
        RecStream.Load(Server.MapPath("~/App_Data/Receiver.xml")) 'My.Resources.Receiver)
        LinkStream.Load(Server.MapPath("~/App_Data/Link.xml")) 'My.Resources.Link)


        Dim lnk As XmlNodeList = LinkStream.SelectNodes("Root/Link")
        Dim RecName As XmlNodeList = RecStream.SelectNodes("Root/Receiver")
        Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/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.MapPath("~/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("ReceiverID") 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.TextBox2.Text.ToString) Then
                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
            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()
0
Comment
Question by:vcharles
  • 17
  • 12
29 Comments
 
LVL 41

Expert Comment

by:graye
ID: 36917352
The problem is with the LINQ query at
            If CheckForItem.Contains(Me.TextBox2.Text.ToString) Then

... 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
0
 

Author Comment

by:vcharles
ID: 36922297
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
0
 

Author Comment

by:vcharles
ID: 36922321
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.TextBox2.Text.ToString)" If you want I can send you the project.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Expert Comment

by:graye
ID: 36924330
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).
0
 

Author Comment

by:vcharles
ID: 36924653
Can you please send me a sample code on how you would achieve the same using XPATH.
Thanks.
0
 
LVL 41

Expert Comment

by:graye
ID: 36925093
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.
'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

Open in new window

0
 

Author Comment

by:vcharles
ID: 36925449
Thanks a million. I will try it and get back to you.
0
 

Author Comment

by:vcharles
ID: 36926952
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[RecieverID=" & Me.TextBox2.Text & "]")

Code:

 Protected Sub Button9_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button9.Click
        DonorStream.Load(Server.MapPath("~/App_Data/Donor.xml")) 'My.Resources.Donor)
        RecStream.Load(Server.MapPath("~/App_Data/Receiver.xml")) 'My.Resources.Receiver)
        LinkStream.Load(Server.MapPath("~/App_Data/Link.xml")) 'My.Resources.Link)


        Dim lnk As XmlNodeList = LinkStream.SelectNodes("Root/Link")
        Dim RecName As XmlNodeList = RecStream.SelectNodes("Root/Receiver")
        Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/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.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[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
    End Sub
0
 
LVL 41

Expert Comment

by:graye
ID: 36927029
Sorry, how about  

LinkStream.SelectNodes
0
 

Author Comment

by:vcharles
ID: 36927033
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[RecieverID=" & Me.TextBox2.Text & "]")
            Dim rID As Integer = Val(n.ChildNodes(0).InnerText)
            Dim dID As Integer = Val(n.ChildNodes(1).InnerText)
0
 

Author Comment

by:vcharles
ID: 36927188
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.MapPath("~/App_Data/Donor.xml")) 'My.Resources.Donor)
        RecStream.Load(Server.MapPath("~/App_Data/Receiver.xml")) 'My.Resources.Receiver)
        LinkStream.Load(Server.MapPath("~/App_Data/Link.xml")) 'My.Resources.Link)


        Dim lnk As XmlNodeList = LinkStream.SelectNodes("Root/Link")
        Dim RecName As XmlNodeList = RecStream.SelectNodes("Root/Receiver")
        Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/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.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 LinkStream.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
    End Sub

Thanks,

Victor
0
 

Author Comment

by:vcharles
ID: 36927249
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
0
 

Author Comment

by:vcharles
ID: 36930347
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).InnerText)), 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.XPathNodeIterator = nav.Select("//ReceiverID[. = Textbox2.Text]/parent::node()/Link")



Code:
 Dim DonorStream As New System.Xml.XPath.XPathDocument(Server.MapPath("~/App_Data/Donor.xml"))
        Dim RecStream As New System.Xml.XPath.XPathDocument(Server.MapPath("~/App_Data/Receiver.xml"))
        Dim LinkStream As New System.Xml.XPath.XPathDocument(Server.MapPath("~/App_Data/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.MapPath("~/App_Data/Link.xml"))
        Dim nav As System.Xml.XPath.XPathNavigator = 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.XPathNodeIterator = nav.Select("//ReceiverID[. = '1']/parent::node()/Link")
        Do While iterator.MoveNext
            ' For Each n As XmlNode In LinkStream.SelectNodes("/root/link[ReceiverID=" & 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 inn  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(0).InnerText})
                End If

            Next

            For Each node As XmlNode In DonorName
                If node.ChildNodes(0).InnerText = dID.ToString Then
                    donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerText})
                    ''donorlst.Rows.Add(New Object() {dID, node.ChildNodes(1).InnerText})
                End If
            Next
        Loop
    End Sub


Thanks,

Victor
0
 
LVL 41

Expert Comment

by:graye
ID: 36930855
I'll work on it later today...
0
 

Author Comment

by:vcharles
ID: 36931108
I'm looking froward to hear back from you,

Thanks for your efforts.

Victor
0
 
LVL 41

Expert Comment

by:graye
ID: 36931284
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...
' 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

Open in new window

0
 
LVL 41

Expert Comment

by:graye
ID: 36931348
See?  XPath is not so bad...   I use it along with LINQ.   So, I'd encourage you to learn both techniques...
0
 
LVL 41

Expert Comment

by:graye
ID: 36931388
Yikes!  I found a typo...

For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/Root/Receiver[ReceiverID='" & ReceiverID & "']")
                                                           ^^^^^^^
0
 

Author Comment

by:vcharles
ID: 36931478
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.MapPath("~/App_Data/Donor.xml"))
            xdLink.Load(Server.MapPath("~/App_Data/Link.xml"))
            xdReceiver.Load(Server.MapPath("~/App_Data/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)
                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[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...")
                    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("/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...")
                    MsgBox("ReceiverID=" & ReceiverID2)
                    MsgBox("Name=" & Name)
                Next
            Next
        End If
    End Sub
0
 

Author Comment

by:vcharles
ID: 36931670
SWhouldn't this be:

For Each xdLink As Xml.XmlNode In xdLink.SelectNodes("/Root/Link[ReceiverID='" & TextBoxText & "']")
 instead of:

For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/Link[ReceiverID='" & TextBoxText & "']")
0
 
LVL 41

Expert Comment

by:graye
ID: 36931801
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?
0
 
LVL 41

Expert Comment

by:graye
ID: 36931815
The XML files are the ones you posted at the top of this thread!
0
 

Author Comment

by:vcharles
ID: 36931995
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.MapPath("~/App_Data/Donor.xml")) 'My.Resources.Donor)
        RecStream.Load(Server.MapPath("~/App_Data/Receiver.xml")) 'My.Resources.Receiver)
        LinkStream.Load(Server.MapPath("~/App_Data/Link.xml")) 'My.Resources.Link)


        Dim lnk As XmlNodeList = LinkStream.SelectNodes("Root/Link")
        Dim RecName As XmlNodeList = RecStream.SelectNodes("Root/Receiver")
        Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/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.MapPath("~/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("ReceiverID") 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.TextBox2.Text.ToString) Then
                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
            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
0
 
LVL 41

Expert Comment

by:graye
ID: 36932073
What?  You're back to your original design... with that LINQ statement that doesn't do what you are expecting it to do?
0
 

Author Comment

by:vcharles
ID: 36932138
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.
0
 

Author Comment

by:vcharles
ID: 36932164
    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()
0
 
LVL 41

Accepted Solution

by:
graye earned 2000 total points
ID: 36932173
OK... here is a combination of the new design and the datatable operations from the old design.

Again... my test app is a windows form application, so it is not a direct drop-in replacement
Public Class Form1

    ' Create the tables with appropriate scope
    Dim dtDonor As DataTable
    Dim dtReceiver As DataTable

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' instanitate the datatables, and create the columns
        dtDonor = New DataTable
        dtDonor.Columns.Add("ID", GetType(String))
        dtDonor.Columns.Add("Name", GetType(String))

        dtReceiver = New DataTable
        dtReceiver.Columns.Add("ID", GetType(String))
        dtReceiver.Columns.Add("Name", GetType(String))
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' 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)

                ' add 'em to the Donor datatable
                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

                Debug.WriteLine("From Reciever.xml...")
                Debug.WriteLine("ReceiverID=" & ReceiverID2)
                Debug.WriteLine("Name=" & Name)

                ' add 'em to the Receiver datatable
                dtReceiver.Rows.Add({ReceiverID2, Name})
            Next
        Next

        ' verify that there's data in the datatables
        Debug.WriteLine("dtDonor.Rows.Count=" & dtDonor.Rows.Count)
        Debug.WriteLine("dtReceiver.Rows.Count=" & dtReceiver.Rows.Count)

        ' bind the datatables to the comboboxs
        ComboBox1.DataSource = dtDonor
        ComboBox1.DisplayMember = "Name"

        ComboBox2.DataSource = dtReceiver
        ComboBox2.DisplayMember = "Name"
    End Sub


End Class

Open in new window

0
 

Author Comment

by:vcharles
ID: 36932365
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("Root/Link").Count
        If (NodeNumber < NodeCount) Then
            NodeNumber += 1   'Move to some next node
            Dim node As XmlNode = LinkStream.SelectSingleNode("Root/Link[" & 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!!!
0
 

Author Closing Comment

by:vcharles
ID: 36932370
THANK YOU!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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.…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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 .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

840 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