Saturday, March 5, 2011

Databinding and Displaying XML Data for Aspnet Developers

Hi friends,In this article i would like to discuss the topic regarding XML,
we all know that, By xml we can create ,retrieve,bind the Database for aspnet Applications.
Create a XML file :
Creating an XML file in Visual Studio 2005 is as simple as picking the XML File option from the Add New Item window,

which gives a standard editor like

   
   
   
   
   
   
   
   
   
   
  


Databinding and Displaying XML Data:

Manually Binding to XML Files
If we are not using an XmlDataSource control, and are using the DataSource property to set the binding,
the simplest way to bind to XML data is to create a DataSet, read the XML into the DataSet, and bind to that

Reading XML into a DataSet

DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("countries.xml"));

GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();

Here the ReadXml method of the DataSet is used to read the file into the data set.
This will infer the schema and construct a table equivalent to the XML data
(you can also use the ReadXmlSchema method to read in a predefined schema).
There is also a WriteXml method to write the internal data out to an XML file;
this is an excellent method of constructing XML files from SQLjust load it into the DataSet using normal commands,
and then use WriteXml to create the file

Using the XmlDataSource Control

To use the data source form of binding, you use a form similar to the SQL and Object data source controls,
defining the control and using the DataSourceID property on the bound control.

The XmlDataSource has three main properties for the loading and selection of data:

DataFile, which is the XML file to load

transformFile, which is an XSLT stylesheet applied to the data before it is passed to any databound controls

XPath, which applies an XPath expression to the data before it is passed to any databound controls

There are other properties, such as those that deal with caching, but these are the three that deal with the data files and selection. So at it's simplest, use of the control becomes:

You can also specify the data inline, by use of the Data section:


   
     
       
     
   



If both the DataFile property and the Data section are present, only the DataFile is used.

In use, you can use any of the controls that support databinding,
but you have to be aware that some of these are designed for relational data,
and XML is generally hierarchical. Thus, binding a grid to XML data might not be the best way to view the data,
but using a hierarchical control such as a TreeView might be. For example:



Countries XML Using Elements

  
    1
    abc   
  
  
    2
    bcd   
  
  
    3
    cdf   
  



Binding Expressions

If you are binding to XML but don't want to use the automatic binding, you can use binding expressions, in a similar way to binding to SQL data. For XML data, you use the XPath expression. For example:

<%# XPath("CountryName") %>

This would bind to the CountryName element, displaying the contents of the element. To bind to attributes, you precede the attribute name with an @ symbol.

<%# XPath("@Make") %>

That's it friends..In this way we can create xml Database
developercode
About the Author
Sayyad is a Software Engineer, Blogger and Founder of Developers Code from India.His articles mainly focus on .Net (Web and Windows based applications), Mobile Technologies (Android,IPhone,BlackBerry) and SEO.

Labels: , , , , , ,

4 Comments:

At April 20, 2011 at 3:31 PM , Anonymous Anonymous said...

Good Post For Beginners

 
At April 20, 2011 at 3:32 PM , Anonymous Francis said...

Nice one dude.....

 
At June 28, 2011 at 10:16 AM , Anonymous Anonymous said...

Good Post For Beginners

 
At August 9, 2011 at 2:39 PM , Anonymous xml editor said...

you can also use software to do the binding for you but ofcourse, there's more skill and satisfaction involved when you can follow the steps in your tutorial, thanks.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home