Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 16th, 2003, 02:00 PM
Registered User
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Binding Results of an Xpath query to a datagrid

Note sure if this is possible but I have the following xml doument:

<?xml version="1.0" encoding="utf-8"?>
    <category type="Computer">
            <name>Compaq Case</name>
            <description>Tower Case With Mouse and Keyboard</description>
            <specs>600MHz with 10Gig Drive 128Megs of Ram</specs>
            <name>Server Tower</name>
            <description>Atlas Case with 300watt Power Supply</description>
            <specs>1.8GHz Athlon 512Megs of Ram. 80Gig Hard Drive</specs>
    <category type="Peripials">
            <name>Altec Lansing Speakers</name>
            <description>Altec Lansing With Sub</description>
            <specs>2 Satilites with Subwoofer 100watts max</specs>

I used the following code to grab all items under the category of "Computer":

 Sub getitems()
        If dlCategory.SelectedItem.Value <> "none" Then

            Dim xdoc As New XmlDocument
            Dim xnodes As XmlNodeList
            Dim xnode As XmlNode
            Dim strCat As String = dlCategory.SelectedItem.Value.Trim


            xnodes = xdoc.DocumentElement.SelectNodes("//category[@type='" & strCat & "']/item")

'For Each loop used for error checking (Wanted to make sure I was grabbing the correct results.

            For Each xnode In xnodes
                Response.Write(xnode.InnerText & "<br/>")

'Problem Need to bind the results to a datagrid.

       End If
    End Sub

There is a drop down list box on the page that is preloaded (from the same xml document) with all the categories. When the user selects an item from the drop down list, the getitems() procedure gets called to grab all the items with the matching category. The problem that I am having is now that I have selected the items. How do I bind the results to a datagrid or a datalist? I know how to get the resluts bound from an SQL database, but this is the first time trying to bind data using XML.

I hope I am posting this to the correct Forum (ie. XML forum vs ADO.NET forum).

Thanks in Advance.

Old November 16th, 2003, 11:28 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

Have you tried binding the node list "xnodes" to the datagrid? I don't think I've ever tried to bind data from XML to a datagrid, but I guess that would be my first approach. Try just binding the nodelist.

The other thing that you might want to look into is loading up a DataSet from the XML. Once you have the XML data in a dataset, it won't be any different than dealing with SQL data. It's all the same in the dataset. When you select a category from the dropdownbox, then you can set filter criteria on the dataset table then bind that table to the datagrid. I'm not sure if the dataset automatically has DataRelations for the implicit relationship between the tables that are created from the XML nodes or not. Like I said, I don't work much with XML data so I can't provide much more than ideas.

Work smarter, not harder.
Old November 17th, 2003, 02:40 AM
Registered User
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

Hello Sir,
     I never tried to bind data from XML. but i know how to fill data from XML to dataset or datagrid. use ReadXML method of 'dataset' object and assign that dataset to datagrid control.

    private ds as new Dataset()
    ds.readXML(path of XML file)
    DataGrid1.datasource = ds


Similar Threads
Thread Thread Starter Forum Replies Last Post
Edit Query Results in Results Grid druid2112 SQL Server 2005 1 June 28th, 2007 08:49 AM
xpath query blitzer XSLT 2 May 19th, 2007 03:54 AM
different results were given for the same query madhusrp SQL Server 2000 2 May 9th, 2006 01:54 AM
Read xpath query from node value jaquing XSLT 2 January 11th, 2006 06:50 PM
DataGrid Binding GrindCrusher Classic ASP Databases 0 February 21st, 2004 05:52 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.