Social Icons

Wednesday, 21 August 2013

Data Binding with WPF : Binding to XML

Introduction:

In this article i will explain How to bind XML Data to Application UI in WPF. What are the different ways to specify the XML Data to Applicaiton, How to bind the xml elements to UI in wpf etc.

Download SourceCode and Video Explaining How to bind Xml Data to ListBox in WPF 

Description:

       In wpf for accessing xml data in your application you can use XmlDataProvider Class. This class provides declarative access to xml data for binding. XmlDataProvider Class provides three ways to access XML Data.

  1. You can embed inline XML Data within the tag inside XmlDataProvider Class.
  2. You can use Source to specify the Uri of the XML file.
  3. You can use Document property to specify the XmlDocument as binding source.
One Important property of XmlDataProvider Class is XPath which defines the level binding source in XML file.

 Embedding Inline XML Data:

           

  
If you see the XAML code XML Data is embed with in tag. This XML file has root element as Authors and child element Author with two attributes name and age.

          So for accessing these XML element in ListBox, i specified the itemsource to AuthorNames, XPath=* means it will returns all the data in XML. We can use XPath to query against the XML data
XPath=*[@age>25] will returns the Authors whose age is greater than 25. In this way query against Data.

Accessing XML Data By Setting Source:

             I Have an XML File as Follows:



I am displaying these data in UI as follows
Accessing XML Data Using XmlDataProvider

Here is the XAML Code:


  In this example i am loading main root elements i combobox, based on the combobox selection ListView is loaded with the Model, Based on the selection in ListView then Brand Price details will be shown. 
Watch this Video explaining How to Bind Xml Data to ListBox in WPF




Conclusion:

        I hope you got some basic details about accessing xml data in wpf and bind it to UI.

       Don't forget to Comment and share it for our friends if you like this post. Thank You.

 

No comments:

Post a comment

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.