RadGrid can use the XmlDataSource control to generate its content from XML and perform data editing operations. Note that automatic update, insert, and delete operations, which work with other data source controls such as AccessDataSource, SqlDataSource, and ObjectDataSource, do not work when using XmlDataSource. If you want to enable editing, you must write custom code to modify data in the XML source file.
The following restrictions apply to editing XML data when using XmlDataSource control:
The XML data must be loaded from an XML file specified with the DataFile property of the XmlDataSource control, and not from an xml string specified in the Data property.
No XSLT transformation can be specified in the Transform or TransformFile properties of the XmlDataSource control.
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.
ProtectedSub RadGrid1_InsertCommand(ByVal source AsObject,ByVal e As Telerik.Web.UI.GridCommandEventArgs)Handles RadGrid1.InsertCommand
Dim gridEditFormItem As GridEditFormItem
gridEditFormItem = e.Item
Dim ht As Hashtable =New Hashtable()
gridEditFormItem.ExtractValues(ht)Dim maxCustomerId AsInteger=0For Each selectedNode As XmlNode In _
XmlDataSource1.GetXmlDocument().SelectNodes("Customers/Customer")Dim customerId AsInteger= _
Integer.Parse("0"+ selectedNode.Attributes("CustomerID").Value)If customerId > maxCustomerId Then
maxCustomerId = customerId
EndIfNext
ht.Add("CustomerID", maxCustomerId +1)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
EndTryEndSubProtectedSub RadGrid1_UpdateCommand(ByVal source AsObject,ByVal e As Telerik.Web.UI.GridCommandEventArgs)Handles RadGrid1.UpdateCommand
Dim gridEditFormItem As GridEditFormItem
gridEditFormItem = e.Item
Dim ht As Hashtable
ht =New Hashtable()
gridEditFormItem.ExtractValues(ht)Dim customerID AsString
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
EndTryEndSubProtectedSub RadGrid1_DeleteCommand(ByVal source AsObject,ByVal e As Telerik.Web.UI.GridCommandEventArgs)Handles RadGrid1.DeleteCommand
Dim gridDataItem As GridDataItem
gridDataItem = e.Item
Dim ht As Hashtable
ht =New Hashtable()
gridDataItem.ExtractValues(ht)Dim customerID AsString
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
EndTryEndSubPrivateFunction ConvertNullToEmpty(ByVal obj AsObject)AsStringIf(obj IsNothing)ThenReturnString.Empty
ElseReturn obj.ToString()EndIfEndFunction