You can use XmlDataSource control and Telerik RadGrid to generate its content from XML and perform data editing operations. Note that automatic update, insert, and delete operations that work with other data source controls (AccessDataSource/SqlDataSource/ObjectDatSource) will not work in this case. You need to write custom code to modify data in the xml source.
The following list outlines some restrictions on editing XML data using XmlDataSource control:
- The XML data must be loaded from an XML file specified with the DataFile property, and not from xml string specified in the Data property.
- No XSLT transformation can be specified in the Transform or TransformFile properties.
- The Save method does not handle concurrent save operations on unique requests. If more than one user is editing an XML file using the XmlDataSource control, there is no guarantee that all users are operating with the same data and that one user will not overwrite changes from another user. It is also possible for a Save operation to fail because another user is writing to the XML file and has an exclusive lock on the file.
Here are some sample code snippets extracted from the relevant online example of the product:
| ASPX/ASCX |
Copy Code |
|
<asp:Label ID="lblMsg" runat="server" ForeColor="Red"></asp:Label> <rad:radgrid Skin= "Web20" id= "RadGrid1" runat= "server" Width= "95%" DataSourceID= "XmlDataSource1" OnInsertCommand="RadGrid1_InsertCommand" OnUpdateCommand="RadGrid1_UpdateCommand" OnDeleteCommand="RadGrid1_DeleteCommand"> <MasterTableView AutoGenerateColumns="false" DataKeyNames="CustomerID" CommandItemDisplay="Top"> <Columns> <rad:GridEditCommandColumn> </rad:GridEditCommandColumn> <rad:GridBoundColumn UniqueName= "CompanyName" DataField= "CompanyName" HeaderText= "CompanyName"> </rad:GridBoundColumn> <rad:GridBoundColumn UniqueName= "ContactName" DataField= "ContactName" HeaderText= "ContactName"> </rad:GridBoundColumn> <rad:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"> </rad:GridButtonColumn> </Columns> </MasterTableView> </rad:radgrid> <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Grid/Data/Xml/XmlDataSourceExampleTemp.xml" > </asp:XmlDataSource> |
| C# |
Copy Code |
|
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) { GridEditFormItem gridEditFormItem = (GridEditFormItem)e.Item; Hashtable ht = new Hashtable(); gridEditFormItem.ExtractValues(ht);
String customerID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][ "CustomerID"].ToString();
XmlNode node = XmlDataSource1.GetXmlDocument().SelectSingleNode( String.Format("Customers/Customer[@CustomerID='{0}']", customerID));
node.Attributes[ "CompanyName"].Value = ConvertNullToEmpty(ht["CompanyName"]); node.Attributes[ "ContactName"].Value = ConvertNullToEmpty(ht["ContactName"]); try { XmlDataSource1.Save(); } catch (Exception ex) { lblMsg.Text = ex.Message; } //refresh the source file RefreshXmlFile(); }
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e) { GridEditFormItem gridEditFormItem = (GridEditFormItem)e.Item; Hashtable ht = new Hashtable(); gridEditFormItem.ExtractValues(ht); ht.Add( "CustomerID", XmlDataSource1.GetXmlDocument().CreateAttribute( "CustomerID"));
XmlNode node = XmlDataSource1.GetXmlDocument().CreateElement( "Customer");
foreach (DictionaryEntry entry in ht) { XmlAttribute attribute = XmlDataSource1.GetXmlDocument().CreateAttribute(entry.Key.ToString()); attribute.Value = ConvertNullToEmpty(entry.Value); node.Attributes.Append(attribute); } XmlDataSource1.GetXmlDocument().SelectSingleNode( "Customers").AppendChild(node); try { XmlDataSource1.Save(); } catch (Exception ex) { lblMsg.Text = ex.Message; } //refresh the source file RefreshXmlFile(); } protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e) { GridDataItem gridDataItem = (GridDataItem)e.Item; Hashtable ht = new Hashtable(); gridDataItem.ExtractValues(ht); String customerID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CustomerID"].ToString(); XmlNode node = XmlDataSource1.GetXmlDocument().SelectSingleNode( String.Format("Customers/Customer[@CustomerID='{0}']", customerID)); XmlNode parent = node.ParentNode; parent.RemoveChild(node); try { XmlDataSource1.Save(); } catch (Exception ex) { lblMsg.Text = ex.Message; } RefreshXmlFile(); } |
| VB.NET |
Copy Code |
|
Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.InsertCommand Dim gridEditFormItem As GridEditFormItem gridEditFormItem = e.Item Dim ht As Hashtable ht = New Hashtable() gridEditFormItem.ExtractValues(ht) ht.Add( "CustomerID", XmlDataSource1.GetXmlDocument().CreateAttribute( "CustomerID"))
Dim node As XmlNode node = XmlDataSource1.GetXmlDocument().CreateElement( "Customer")
Dim entry As DictionaryEntry For Each entry In ht Dim attribute As XmlAttribute attribute = XmlDataSource1.GetXmlDocument().CreateAttribute(entry.Key.ToString()) attribute.Value = ConvertNullToEmpty(entry.Value) node.Attributes.Append(attribute) Next entry
XmlDataSource1.GetXmlDocument().SelectSingleNode( "Customers").AppendChild(node) Try XmlDataSource1.Save() Catch ex As Exception lblMsg.Text = ex.Message End Try RefreshXmlFile() End Sub
Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.UpdateCommand Dim gridEditFormItem As GridEditFormItem gridEditFormItem = e.Item Dim ht As Hashtable ht = New Hashtable() gridEditFormItem.ExtractValues(ht)
Dim customerID As String customerID = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)( "CustomerID").ToString()
Dim node As XmlNode node = XmlDataSource1.GetXmlDocument().SelectSingleNode( String.Format("Customers/Customer[@CustomerID='{0}']", customerID))
node.Attributes( "CompanyName").Value = ConvertNullToEmpty(ht("CompanyName")) node.Attributes( "ContactName").Value = ConvertNullToEmpty(ht("ContactName")) Try XmlDataSource1.Save() Catch ex As Exception lblMsg.Text = ex.Message End Try RefreshXmlFile() End Sub Protected Sub RadGrid1_DeleteCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.DeleteCommand Dim gridDataItem As GridDataItem gridDataItem = e.Item Dim ht As Hashtable ht = New Hashtable() gridDataItem.ExtractValues(ht) Dim customerID As String customerID = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("CustomerID").ToString() Dim node As XmlNode node = XmlDataSource1.GetXmlDocument().SelectSingleNode(String.Format("Customers/Customer[@CustomerID='{0}']", customerID)) Dim parent As XmlNode parent = node.ParentNode parent.RemoveChild(node) Try XmlDataSource1.Save() Catch ex As Exception lblMsg.Text = ex.Message End Try RefreshXmlFile() End Sub |