XML data problem
I'm 开发者_运维知识库new to XML I've been supplied an xml document which looks something like this
<items>
<Bookings>
<Name>Mr Pf_Test_15033</Name>
<Total>315</Total>
<Products>
<Flight>
<Adults>2</Adults>
</Flight>
<Ferry>
<Adults>2</Adults>
</Ferry>
</Products>
</Bookings>
</items>
I'm trying to use ASP.Net to Databind my xml to a datagrid
So far I've got this
Sub Page_Load
Dim dstMenu As DataSet
dstMenu = New DataSet ()
dstMenu.ReadXml(MapPath("getbooking.xml"))
rptItems.DataSource = dstMenu
rptItems.DataBind ()
End Sub
And this in the body
<asp:DataGrid ID="rptItems" AutoGenerateColumns="true" runat="server">
</asp:DataGrid>
Now this works to a certain degree the result I get only shows the columns Name
and Total
but I need it to show the Products
information with the Flight
and Ferry
info too.
I've looked around but can't seem to find anyone using XML in this way.
Is this possible?
You are asking for a multi-level data gird. Here is an article that could be of help to you
Did you consider other means of displaying data rather than using a hierarchial datagrid?
Like you could load Bookings in a ListBox and when a user selects a booking display the ferry and flight details in a FormView / DetailsView.
But if you think that using multi-level datagrid is the right one for you, I've modified the sample from the above blog to work with xml. put the below code in your aspx.cs file
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
_dataSet = new DataSet();
_dataSet.ReadXml(Server.MapPath("Bookings.xml"), XmlReadMode.InferSchema);
DataView dv = new DataView(_dataSet.Tables["Bookings"]);
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
}
protected void HandleOnDataGridItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
DataGrid productsDataGrid = (DataGrid)e.Item.FindControl("ProductsDataGrid");
productsDataGrid.DataSource = new DataView(_dataSet.Tables["Products"]);
productsDataGrid.DataBind();
}
}
protected void HandleOnProductsDataGridItemDataBound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
DataGrid ferryDataGrid = (DataGrid)e.Item.FindControl("FerryDataGrid");
ferryDataGrid.DataSource = new DataView(_dataSet.Tables["Ferry"]);
ferryDataGrid.DataBind();
DataGrid flightDataGrid = (DataGrid)e.Item.FindControl("FlightDataGrid");
flightDataGrid.DataSource = new DataView(_dataSet.Tables["Flight"]);
flightDataGrid.DataBind();
}
}
put this in aspx file
<asp:DataGrid ID="DataGrid1" runat='server' AutoGenerateColumns='false'
OnItemDataBound='HandleOnDataGridItemDataBound'
>
<Columns>
<asp:BoundColumn DataField='Name' HeaderText='Booking'/>
<asp:TemplateColumn HeaderText="Products">
<ItemTemplate>
<asp:DataGrid ID='ProductsDataGrid' runat='server' AutoGenerateColumns='false'
OnItemDataBound='HandleOnProductsDataGridItemDataBound'>
<Columns>
<asp:BoundColumn DataField="Details" HeaderText='Product details' />
<asp:TemplateColumn>
<ItemTemplate>
<asp:DataGrid ID="FerryDataGrid" runat="server">
</asp:DataGrid>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:DataGrid ID="FlightDataGrid" runat="server">
</asp:DataGrid>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
and I've modified your xml a bit to see how it shows up with more fields and multiple bookings nodes. You have to have some kind of relationship between the Bookings - Products -Ferry and Flight to set row filters.
<items>
<Bookings>
<Name>Mr Pf_Test_15033</Name>
<Total>315</Total>
<Products>
<Details>Flight and Ferry bookings</Details>
<Flight>
<Name>Flight1</Name>
<Adults>2</Adults>
</Flight>
<Ferry>
<Name>Ferry1</Name>
<Adults>2</Adults>
</Ferry>
</Products>
</Bookings>
</items>
You can use a DataList instead (less overhead) with an Xml Web Control inside the ItemTemplate. Write an XSLT to transform the xml into whatever you like. This will be faster and more flexible in the long run, though your brain may explode trying to write the xslt. The code below is the ItemDataBound Event sample:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Retrieve the controls in the current DataListItem.
System.Web.UI.WebControls.Xml xmlLogEntry = (System.Web.UI.WebControls.Xml)e.Item.FindControl("XmlLogEntry");
//YOU WILL HAVE TO CREATE A HELPER METHOD THAT BINDS THE DATA
string xml = Convert.ToString(
((DataRowView)e.Item.DataItem).Row.ItemArray[6].ToString());
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
xmlLogEntry.XPathNavigator = xmlDoc.CreateNavigator();
xmlLogEntry.TransformSource = @"~/XSLT/Transform.xslt";
}
精彩评论