views:

79

answers:

1

Hello,

Please help me in reading this multilevel xml to a RadGrid. 1) I am planning not to use Aspx for this 2) The node named Item would eventually change as per app Requirements. So, I don't want to restrict my xpath to something like "//Product/Item"

<Products>  
      <Product ProductID="1">  
        <Item ItemID="1">  
          <ProductNo>1234</ProductNo>  
          <Description>  
            <ManufacDate>20100526T12:00:01.012</ManufacDate>  
            <ManufacID>72</ManufacID>  
          </Description>  
        </Item>  
        <Item ItemID="2">  
          <ProductNo>1235</ProductNo>  
          <Description>  
            <ManufacDate>20100527T12:00:01.012</ManufacDate>  
            <ManufacID>72</ManufacID>  
          </Description>  
        </Item>  
        <Item ItemID="3">  
          <ProductNo>1236</ProductNo>  
          <Description>  
            <ManufacDate>20100528T12:00:01.012</ManufacDate>  
            <ManufacID>66</ManufacID>  
          </Description>  
        </Item>  
     </Product></Products>

I am reading this xml from codebehind and iterating through xmlnodes to display in the grid. Please find my snippet below:

Dim root as new xmldocument()  

' After loading the above xml into root   

Dim ndl As XmlNodeList = root.SelectNodes("//Products")  

 For Each chNode As XmlNode In ndl  

                Dim chObjNode As XmlNodeList = root.SelectNodes("descendant::node()[ancestor::Product[@ProductID=""1""]]")   
                For Each XNode As XmlNode In chObjNode  
                    ds.ReadXml(New XmlNodeReader(XNode))  
                Next 
            Next 
radgrid1.datasource = ds  
radgrid1.databind()

Taking this into reference. It would be great if you can help me correct or provide a solution to this issue without converting to XSL. I need the Columns: ItemID, ProductNo, indented Description with ManufacDate and ManufacID. I m not sure how to get this displayed.

Thanks

A: 

I think the easiest thing for you to do in this case would be to actually get your XML data into a DataSet and from there bind to the RadGrid.

I actually took your sample XML above and used the following markup (note the AutoGenerateHierarchy property is set to true just for ease of testing):

    <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateHierarchy="true">
    </telerik:RadGrid>

In the code-behind I did the following:

Protected Sub RadGrid1_NeedDataSource(ByVal sender As Object, ByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
    RadGrid1.DataSource = GetHierarchicalXmlData()
End Sub

Private XMLDataSet As DataSet
Private Function GetHierarchicalXmlData() As DataSet
    If XMLDataSet IsNot Nothing Then
        Return XMLDataSet
    End If

    XMLDataSet = New DataSet()
    Dim reader As New XmlTextReader(Server.MapPath("SampleXML.xml"))
    XMLDataSet.ReadXml(reader)


    ' in case you do not want a three-level hierarchy:
    ' this just merges the item and the description table
    ' and then removes the description table            

    XMLDataSet.Tables(1).Merge(XMLDataSet.Tables(2))
    XMLDataSet.Relations.Remove("Item_Description")
    XMLDataSet.Tables(2).Constraints.Clear()
    XMLDataSet.Tables.RemoveAt(2)

    Return XMLDataSet
End Function

What the above code does should be pretty straight forward (although I'm not a VB.NET guy ;)), but it just uses the DataSet's ReadXML function to take in a XmlTextReader that has your XML file.

Now the way your XML is set up this would hierarchically create three levels (Product -> Item -> Description) and in case you did not want this I included some sample code of how to merge the Item and Description tables and then just remove the Description table.

This is of course just a small sample but I believe it takes care of everything.

Carl B.