programming4us
programming4us
WEBSITE

ASP.NET 4.0 : Data Source Components (part 10) - The XmlDataSource Class

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
6.2 Programming Interface of SiteMapDataSource

Table 12 details the properties available in the SiteMapDataSource class.

Table 12. Properties of SiteMapDataSource
PropertyDescription
ProviderIndicates the site map provider object associated with the data source control.
ShowStartingNodeTrue by default, indicates whether the starting node is retrieved and displayed.
SiteMapProviderGets and sets the name of the site map provider associated with the instance of the control.
StartFromCurrentNodeFalse by default, indicates whether the node tree is retrieved relative to the current page.
StartingNodeOffsetGets and sets a positive or negative offset from the starting node that determines the root hierarchy exposed by the control. This property is set to 0 by default.
StartingNodeUrlIndicates the URL in the site map in which the node tree is rooted.

By default, the starting node is the root node of the hierarchy, but you can change the starting node through a pair of mutually exclusive properties—StartFromCurrentNode and StartingNodeUrl. If you explicitly indicate the URL of the page that should appear as the root of the displayed hierarchy, make sure the StartFromCurrentNode property is false. Likewise, if you set StartFromCurrentNode to true, ensure the StartingNodeUrl property evaluates to the empty string.

When properly used, the StartingNodeOffset property lets you restrict the nodes of the site map that are actually displayed. The default value of 0 indicates that the root hierarchy exposed by the SiteMapDataSource control is the same as the starting node. A value greater than 0 goes that number of levels down in the hierarchy, proceeding from the root to the requested node, and uses the node found as the root. Looking at the sample site map we considered earlier, if you request sitemapinfo.aspx with an offset of 1, the displayed hierarchy will be rooted in the Samples node—that is, one level down the real root. If you set it to 2, the effective root will be the Part 2 node. A negative offset, on the other hand, ensures that the specified number of child levels will be displayed if possible.

The SiteMapDataSource class features a couple of properties that relate to the site map provider—SiteMapProvider and Provider. The former specifies the name of the site map provider to use; the latter returns a reference to the object.

7. The XmlDataSource Class

The XmlDataSource control is a special type of data source control that supports both tabular and hierarchical views of data. The tabular view of XML data is just a list of nodes at a given level of a hierarchy, whereas the hierarchical view shows the complete hierarchy. An XML node is an instance of the XmlNode class; the complete hierarchy is an instance of the XmlDocument class. The XML data source supports only read-only scenarios.

Important

The XmlDataSource control is unique in that it is the only built-in data source control to implement both IDataSource and IHierarchicalDataSource interfaces. For both interfaces, though, the control doesn’t go further than implementing the Select method. Hence, the XmlDataSource control is not suitable for Web applications using read/write XML data stores, as it doesn’t support methods such as Delete, Insert, and Update.


Programming Interface of XmlDataSource

Table 13 details the properties of the XmlDataSource control.

Table 13. Properties of XmlDataSource
PropertyDescription
CacheDurationIndicates, in seconds, how long the data should be maintained in the cache.
CacheExpirationPolicyIndicates whether the cache duration is absolute or sliding. If the duration is absolute, data is invalidated after the specified number of seconds. If the duration is sliding, data is invalidated if it is not used for the specified duration.
CacheKeyDependencyIndicates the name of a user-defined cache key that is linked to all cache entries created by the data source control. By letting the key expire, you can clear the control’s cache.
DataContains a block of XML text for the data source control to bind.
DataFileIndicates the path to the file that contains data to display.
EnableCachingEnables or disables caching support.
TransformContains a block of XSLT text that will be used to transform the XML data bound to the control.
TransformArgumentListA list of input parameters for the XSLT transformation to apply to the source XML.
TransformFileIndicates the path to the .xsl file that defines an XSLT transformation to be performed on the source XML data.
XPathIndicates an XPath query to be applied to the XML data.

The XmlDataSource control can accept XML input data as a relative or absolute filename assigned to the DataFile property or as a string containing the XML content assigned to the Data property. If both properties are set, DataFile takes precedence. Note that the Data property can also be set declaratively through the <Data> tag. Furthermore, the contents assigned to Data—a potentially large chunk of text—are stored in the view state regardless of the caching settings you might have. If you bind the control to static text, the risk is that you move the XML data back and forth with the page view state while keeping it also stored in the cache for faster access. If you use Data and enable caching, consider disabling the view state for the control. (It should be noted, though, that disabling the view state on a control usually affects more than one property.)

If caching is enabled and you change the value of the DataFile or Data property, the cache is discarded. The DataSourceChanged event notifies pages of the event.

Displaying XML Data

The XmlDataSource control is commonly bound to a hierarchical control, such as the TreeView or Menu. (These are the only two built-in hierarchical controls we have in ASP.NET, but others can be created by developers and third-party vendors.)

To understand how the XML data source works, consider a file that is a kind of home-grown XML representation of a DataSet—the Employees table of Northwind:

<MyDataSet>
<NorthwindEmployees>
<Employee>
<employeeid>1</employeeid>
<lastname>Davolio</lastname>
<firstname>Nancy</firstname>
<title>Sales Representative</title>
</Employee>
...
<NorthwindEmployees>
<MyDataSet>

Next you bind this file to an instance of the XmlDataSource control and the data source to a tree view:

<asp:XmlDataSource runat="server" ID="XmlSource"
DataFile="~/App_Data/employees.xml" />
<asp:TreeView runat="server" DataSourceId="XmlSource">
</asp:TreeView>

The result (which is not as useful as it could be) is shown in Figure 5.

Figure 5. The layout (rather than contents) of the bound XML file displayed using a TreeView control.


To display data in a way that is really helpful to users, you need to configure node bindings in the tree view:

<asp:TreeView runat="server" DataSourceId="XmlSource">
<DataBindings>
<asp:TreeNodeBinding Depth="3" DataMember="employeeid"
TextField="#innertext" />
<asp:TreeNodeBinding Depth="3" DataMember="lastname"
TextField="#innertext" />
<asp:TreeNodeBinding Depth="3" DataMember="firstname"
TextField="#innertext" />
<asp:TreeNodeBinding Depth="3" DataMember="title"
TextField="#innertext" />
</DataBindings>
</asp:TreeView>

The <DataBindings> section of the TreeView control lets you control the layout and the contents of the tree nodes. The <TreeNodeBinding> node indicates the 0-based depth (attribute Depth) of the specified XML node (attribute DataMember), as well as which attributes determine the text displayed for the node in the tree view and the value associated with the node. The TextField attribute can be set to the name of the attribute to bind or #innertext if you want to display the body of the node. Figure 6 provides a preview.

Figure 6. XML data bound to a TreeView control.


There’s a lot more to know about the TreeView configuration. I delve into that in Programming Microsoft ASP.NET 2.0 Applications: Advanced Topics.

The contents returned by the XmlDataSource control can be filtered using XPath expressions:

<asp:xmldatasource runat="server" ID="XmlSource"
DataFile="~/App_Data/employees.xml"
XPath="MyDataSet/NorthwindEmployees/Employee" />

The preceding expression displays only the <Employee> nodes, with no unique root node in the tree view. XPath expressions are case-sensitive.

Note

The XmlDataSource control automatically caches data, as the EnableCaching property is set to true by default. Note also that by default the cache duration is set to 0, which means an infinite stay for data. In other words, the data source will cache data until the XML file that it depends on is changed.


Transforming XML Data

The XmlDataSource class can also transform its data using Extensible Stylesheet Language Transformations (XSLT). You set the transform file by using the TransformFile property or by assigning the XSLT content to the string property named Transform. Using the TransformArgumentList property, you can also pass arguments to the style sheet to be used during the XSL transformation. An XSL transformation is often used when the structure of an XML document does not match the structure needed to process the XML data. Note that once the data is transformed, the XmlDataSource becomes read-only and the data cannot be modified or saved back to the original source document.

Other  
  •  ASP.NET 4.0 : Data Source Components (part 8) - The LinqDataSource Class -Lazy Loading and Prefetch, Updating Data
  •  ASP.NET 4.0 : Data Source Components (part 7) - The LinqDataSource Class - Selecting, Sorting, and Filtering Data
  •  ASP.NET 4.0 : Data Source Components (part 6) - The LinqDataSource Class - The Goal of LinqDataSource , Programming Interface of the LinqDataSource Control
  •  ASP.NET 4.0 : Data Source Components (part 5) - The ObjectDataSource Control - Setting Up for Paging , Updating and Deleting Data
  •  ASP.NET 4.0 : Data Source Components (part 4) - The ObjectDataSource Control - Using Parameters, Caching Data and Object Instances
  •  ASP.NET 4.0 : Data Source Components (part 3) - The ObjectDataSource Control - Implementing Data Retrieval
  •  ASP.NET 4.0 : Data Source Components (part 2) - The SqlDataSource Control
  •  ASP.NET 4.0 : Data Source Components (part 1) - Internals of Data Source Controls
  •  ASP.NET 4.0 : Data-Binding Expressions (part 2) - Other Data-Binding Methods
  •  ASP.NET 4.0 : Data-Binding Expressions (part 1) - Simple Data Binding
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us