Onlineatoz.net | ToolBox Data Control XmlDataSource

ToolBox Data Control XmlDataSource

XmlDataSource Web Server Control

in ASP.NET environment,This article describes you how to work with XmlDataSource Web Control.The XmlDataSource control provides the way to binding data to data-bound controls using XML data. By using XmlDataSource Control in DOT.Net,You can display the data with view of both hierarchical and tabular, although the XmlDataSource control is typically used to display hierarchical XML data in read-only scenarios.

Retrieving Data Using the XmlDataSource Control

The XmlDataSource control represents the attributes of XML elements that means each node of Xml file as bindable columns.The XmlDataSource loads XML data from an XML file specified using the DataFile property. XML data can also be loaded from a string using the Data property. If you want to bind to values that are not attributes, you can specify a transformation using an Extensible Sylesheet Language (XSL) style sheet. In control templates, such as in a FormView or GridView controls, you can also bind a control in a template to XML data using the XPath data-binding function.

The following code snippets shows an XmlDataSource with a TreeView control

in .aspx file

<html  >
  <head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form2" runat="server">
      <asp:XmlDataSource
        id="DS1"
        runat="server"
        DataFile="~/App_Data/smplXml.xml" />

      <asp:TreeView
        id="Tree1"
        runat="server"
        DataSourceID="DS1">
        <DataBindings>
          <asp:TreeNodeBinding DataMember="UserId"    TextField="#InnerText" />
          <asp:TreeNodeBinding DataMember="UserName"   TextField="#InnerText" />
        </DataBindings>
      </asp:TreeView>

    </form>
  </body>
</html>

in .xml file,

<?xml version="1.0" encoding="utf-8" ?>
<People>
  <Person>
    <Name>
      <UserName>Rock</UserName>
    </Name>
    <Address>
      <Street>345, Navin St.</Street>
      <City>Dallas</City>
      <Region>America</Region>
      <ZipCode>23652</ZipCode>
    </Address>
    <Job>
      <Title>PL</Title>
      <Description>Develops company strategies.</Description>
    </Job>
  </Person>

  <Person>
    <Name>
      <UserName>Sarfudeen</UserName>
    </Name>
    <Address>
      <Street>4 North St.</Street>
      <City>Banglore</City>
      <Region>IND</Region>
      <ZipCode>12365</ZipCode>
    </Address>
    <Job>
      <Title>Doctor</Title>
      <Description>Heart spl.</Description>
    </Job>
  </Person>
</People>                    
                    
 
Transforming XML Data Using the XmlDataSource Control

You must offer an XSL for the XmlDataSource control when you need transform the xml data at earlier to displayed it by Data-bound control such as Gridview,Detailsview... For XML data, you can loading the style sheet from a file which is exists in the application path, which you specify using the TransformFile property. However, you can also load the style sheet directly from a string using the Transform property.

The following code example shows an XmlDataSource and a TreeView control bound to it.

in .aspx file,

<html  >
  <head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form2" runat="server">
      <asp:XmlDataSource
        id="DS1"
        runat="server"
        TransformFile="~/App_Data/smplXsl.xsl"
        DataFile="~/App_Data/smplXml.xml" />

      <asp:TreeView
        id="Tree1"
        runat="server"
        DataSourceID="DS1">
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Name"   TextField="#InnerText" />
        </DataBindings>
      </asp:TreeView>

    </form>
  </body>
</html>                    
                    

in .xml file,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="People">
  <Names>
    <xsl:apply-templates select="Person"/>
  </Names>
</xsl:template>

<xsl:template match="Person">
  <xsl:apply-templates select="Name"/>
</xsl:template>

<xsl:template match="Name">
  <name><xsl:value-of select="UserName"/>
</xsl:template>
</xsl:stylesheet>                   
                    
 
Editing XML Data Using the XmlDataSource Control

in DOT.Net environment,we can modify the XML data even which is displaying in a Data-bound control.because of The XmlDataSource control is commonly used in read-only data scenarios. Note that automatic update, insert, and delete operations that work with other data source controls will not work. You must write custom code to modify data using the XmlDataSource control.

Using GetXmlDocument method to retrieve an XmlDocument object, which is an in-memory representation of the XML data for edit the XML data.You make sure that the following restrictions when editing XML data using the XmlDataSource control:

  • The XML data must be loaded from an XML file specified with the DataFile property, and not from a string of XML 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 by different 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 no guarantee that one user will not overwrite changes from another user.
Filtering of XmlDataSource Control

By using XPath expression,you can filter the Xml data.Because, By default, the XmlDataSource control exposes all of the XML data specified by the DataFile or Data properties. However, The XPath property allows you to specify an XPath filter expression that is applied after XML data is loaded and has had any transformations applied to it.

Caching of XmlDataSource Control

By default, caching feature is enabled for the XmlDataSource control Because of avoiding the time consuming.opening and reading an XML file every time can affect the performance of your application. Therefore,Caching lets you reduce the processing load on your server at the expense of memory on the Web server;


Posted by: Admin
Posted on: 9/13/2011 at 6:48 PM
Categories: Asp.net
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed