Subject: Binding Results of an Xpath query to a datagrid
Posted By: cowa Post Date: 11/16/2003 1:00:34 PM
Note sure if this is possible but I have the following xml doument:

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


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

            xdoc.Load(Server.MapPath("maintain/SaleItems.xml"))

            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/>")
            Next

'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.
-COWA-



Reply By: planoie Reply Date: 11/16/2003 10:28:55 PM
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.

Peter
------------------------------------------------------
Work smarter, not harder.
Reply By: godinaraja Reply Date: 11/17/2003 1:40:17 AM
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.

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

Regards,
Raja.


Go to topic 6560

Return to index page 1004
Return to index page 1003
Return to index page 1002
Return to index page 1001
Return to index page 1000
Return to index page 999
Return to index page 998
Return to index page 997
Return to index page 996
Return to index page 995