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>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)
I need the Columns: ItemID, ProductNo, indented Description with ManufacDate and ManufacID. I m not sure how to get this displayed.
Thanks,
