This is a migrated thread and some comments may be shown as answers.

How to read multilevel xml

3 Answers 102 Views
Grid
This is a migrated thread and some comments may be shown as answers.
smith spd
Top achievements
Rank 1
smith spd asked on 28 Jul 2010, 08:36 PM
Hello Telerik Team,

Taking this and this into reference.
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
       <Product ProductID="2">
        <Item ItemID="1">
          <ProductNo>1254</ProductNo>
          <Description>
            <ManufacDate>20101226T12:00:01.012</ManufacDate>
            <ManufacID>1232</ManufacID>
          </Description>
        </Item>
        <Item ItemID="2">
          <ProductNo>1T#2345</ProductNo>
          <Description>
            <ManufacDate>20100520T12:00:01.012</ManufacDate>
            <ManufacID>474#</ManufacID>
          </Description>
        </Item>
        <Item ItemID="3">
          <ProductNo>124576</ProductNo>
          <Description>
            <ManufacDate>20010518T12:00:01.012</ManufacDate>
            <ManufacID>T3455</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:

Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Init
        DefineGridStructure()
    End Sub
  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim Grid1 As RadGrid = CType(PlaceHolder1.FindControl("RadGrid1"), RadGrid)
        RadAjaxManager1.AjaxSettings.AddAjaxSetting(Grid1, Grid1)
        If Not IsPostBack Then
            Grid1.SelectedIndexes.Add(1, 0, 1)
            'Grid1.MasterTableView.Items(0).Expanded = True
            'Grid1.MasterTableView.Items(0).ChildItem.NestedTableViews(0).Items(0).Expanded = True
        End If
    End Sub
   

    Public Sub DefineGridStructure() 'Handles RadGrid1.NeedDataSource
        Dim RadGrid1 As RadGrid = New RadGrid

 

        Dim xmlDoc As New System.Xml.XmlDocument()
        xmlDoc.Load(Server.MapPath("OutTypeID6.xml"))

        Dim ndList As System.Xml.XmlNodeList = xmlDoc.SelectNodes("//Product/node()[descendant::node()]")
        Dim nd As System.Xml.XmlNode = xmlDoc.SelectSingleNode("//Product/node()[descendant::node()]")

        Dim ds As New DataSet()
        ds.ReadXml(New StringReader(nd.OuterXml))

        RadGrid1.ID = "RadGrid1"
        RadGrid1.DataSource = ds

        RadGrid1.Width = Unit.Percentage(98)
        RadGrid1.AllowPaging = True
        RadGrid1.AllowSorting = True
        RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric
        RadGrid1.AutoGenerateColumns = False
        RadGrid1.ShowStatusBar = True

        RadGrid1.MasterTableView.DataKeyNames = New String() {nd.Name}

        Dim boundColumn As GridBoundColumn

        Dim tableView As New GridTableView(RadGrid1)
        Dim relationFields As GridRelationFields = New GridRelationFields()
        For Each xmlnode As System.Xml.XmlNode In ndList
            For Each XNode As System.Xml.XmlNode In xmlnode.ChildNodes
                boundColumn = New GridBoundColumn
                boundColumn.DataField = XNode.Name 'XNode.FirstChild.Value
                boundColumn.HeaderText = XNode.Name
                RadGrid1.MasterTableView.Columns.Add(boundColumn)

                If XNode.FirstChild.ChildNodes.Count > 0 Then
                    tableView.DataSource = ds.Tables(1)
                    For Each chNode As System.Xml.XmlNode In XNode.ChildNodes
                        tableView.DataKeyNames = New String() {chNode.Name}
                        tableView.Width = Unit.Percentage(100)
                        relationFields.MasterKeyField = XNode.ParentNode.Name 'chNode.ParentNode.Name
                        relationFields.DetailKeyField = XNode.ParentNode.Name 'chNode.ParentNode.Name
                        tableView.ParentTableRelation.Add(relationFields)
                        RadGrid1.MasterTableView.DetailTables.Add(tableView)

                        boundColumn = New GridBoundColumn
                        boundColumn.DataField = chNode.Name 'XNode.FirstChild.InnerText
                        boundColumn.HeaderText = chNode.Name
                        tableView.Columns.Add(boundColumn)

                    Next
                    tableView.Caption = XNode.Name
                    tableView.DataBind()
                End If
                tableView.Width = Unit.Percentage(100)
            Next
        Next
        RadGrid1.DataSource = ds
        RadGrid1.Rebind()

        Me.PlaceHolder1.Controls.Add(RadGrid1)

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,

3 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 03 Aug 2010, 07:55 AM
Hello Smith,

To achieve the desired functionality you could try binding the RadGrid to data programmatically into NeedDataSource and DetailTableDataBind events. On the following link you could find online example of programmatic binding:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx
On NeedDataSource event you could read you a top level nodes of the xml and into DetailTableDataBoud you could try getting the sublevel nodes.
Also please check out the following links:
http://support.microsoft.com/kb/307548
http://onjava.com/pub/a/onjava/2005/01/12/xpath.html
http://www.stardeveloper.com/articles/display.html?article=2009031001&page=1
http://msdn.microsoft.com/en-us/magazine/cc164116.aspx
http://www.codedigest.com/Articles/ASPNET/342_Using_XPath_Expression_to_Access_or_Read_XML_document_in_ASPNet.aspx

Regards,
Radoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
smith spd
Top achievements
Rank 1
answered on 03 Aug 2010, 05:34 PM
Hello Radoslav,

I am trying a lot to get this working.
Can you provide me with a working solution for the above xml? (using my code snippet)

Thanks,
0
Radoslav
Telerik team
answered on 06 Aug 2010, 11:15 AM
Hi Smith,

To achieve the desired functionality you could try using the hierarchical data-binding using DetailTableDataBind event. Also it will be helpful if you send us a simple runnable example demonstrating the problem. You could open a formal support ticket from your Telerik account and attach a ZIP file there. In that way we can reproduce and pinpoint the problems you're facing on our side, understand the logic of your application and provide a solution.

Regards,
Radoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
smith spd
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
smith spd
Top achievements
Rank 1
Share this question
or