How to bind XML File Data in WPF Applications

By
Hi friend, Today i will show you "how to bind XML file data in WPF Applications". This is very helpful concepts to bind any file data in WPF applications. Here you will also learn "how to write a XML codes (Syntax of XML) ". I have created a separate XML file (MY_XMLFile1.xml) and written some XML codes.In this, i have written some XML codes with XAML file also. In this post i have implemented Stack Panel and Grid Panel layout controls.You can implement it with Canvas, Dock Panel and wrap panel also.You can design your layout controls according to requirements .WPF provides more features to make complex design with the help of XAML codes.  If you want to understand complete XML binding concepts,download whole application from below and run it on your visual studio. I hope you will enjoy in this post.     
There are some steps to implement this concepts as given below:- 

Step 1:- First Open your visual studio --> File-->New -->Project --> WPF Application --> OK --> Now open Solution Explorer -->Add a  New XML File(MY_XMLFile1.xml) --> Write XML codes in this file as given below:-


<?xml version="1.0" encoding="utf-8" ?>
<Student_Records>
    <student Branch ="IT">
      <name> Ram </name>
      <age>23</age>
      <mobile>941520121</mobile>
      <city>Delhi</city>
    </student>

  <student Branch ="CS">
    <name> Sandeep </name>
    <age>20</age>
    <mobile>901520131</mobile>
    <city>Agra</city>
  </student>

  <student Branch ="IT">
    <name> Sachin  </name>
    <age>27</age>
    <mobile>961920131</mobile>
    <city>Mumbai</city>
  </student>

  <student Branch ="Me">
    <name> Neha </name>
    <age>26</age>
    <mobile>940020321</mobile>
    <city>Lucknow</city>
  </student>

  <student Branch ="Me">
    <name> Mahesh</name>
    <age>27</age>
    <mobile>900001321</mobile>
    <city>Bangalore</city>
  </student>
  
</Student_Records>
Step 2:-  Now write the XAML Codes in MainWindow.xaml as shown below:-

<Window x:Class="XML_Bindings.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <XmlDataProvider x:Key="Student_Data" Source="/MY_XMLFile1.xml" XPath="Student_Records">
            </XmlDataProvider>
        </Grid.Resources>
        <StackPanel Orientation="Vertical" Margin="5,5,0,0"  Width="386" Height="248" VerticalAlignment="Top" HorizontalAlignment="Left">
            <StackPanel Orientation="Horizontal" Height="36" Width="306">
                <Label Content="Trade" Margin="0"  Width="60"/>
                <Label Content="Name" Width="90"/>
                <Label Content="Age" Width="90"/>
                <Label Content="City" Width="80"/>
            </StackPanel>
            <ListBox x:Name="list_data" Width="311" ItemsSource="{Binding Source={StaticResource Student_Data},XPath=*}" Height="128">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                  <StackPanel Orientation="Horizontal">
                  <TextBlock Text="{Binding XPath=@Branch}"Width="60" Grid.Column="0"/>
                  <TextBlock Text="{Binding XPath=name}" Width="90"  Grid.Column="1"/>
                  <TextBlock Text="{Binding XPath=age}"  Width="90"  Grid.Column="2" />
                  <TextBlock  Text="{Binding XPath=city}" Width="80"  Grid.Column="3" />
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Grid>
</Window>

Step 3:- Now add a New Window(WPF) --> Write the combine codes of XAML + XML in Window1.xaml as shown below:-

<Window x:Class="XML_Bindings.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="383">
    <Grid>
        <Grid.Resources>
            <XmlDataProvider x:Key="Student_Data" XPath="Student_Records">
                <x:XData>
                    <Student_Records xmlns="">
                        <Student  name="Ram" age="23" mobile="980987098" city="Delhi"/>
                        <Student  name="Neha" age="25" mobile="900987098" city="kolkata"/>
                        <Student  name="Sachin" age="25" mobile="780987098" city="US" />
                        <Author name="Mohan" age="40" mobile="980987097" city="UK" />
                        <Author name="Karan" age="54"/>
                    </Student_Records>
                </x:XData>
            </XmlDataProvider>
        </Grid.Resources>
        <ListBox  x:Name="list_data" ItemsSource="{Binding Source={StaticResource Student_Data}, XPath=*}"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50"/>
                            <ColumnDefinition Width="50"/>
                            <ColumnDefinition Width="80"/>
                            <ColumnDefinition Width="100"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding XPath=@name}" Grid.Column="0" />
                        <TextBlock Text="{Binding XPath=@age}"  Grid.Column="1" />
                        <TextBlock Text="{Binding XPath=@mobile}" Grid.Column="2" />
                        <TextBlock Text="{Binding XPath=@city}"  Grid.Column="3" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

Step 4:- Now Run the Application (press F5)-->You will see the following Output as shown below:
xml_output

Note:- You can see the output of separate xml file (MY_XMLFile1.xml).
Step 5:- Now Press Proceed Next Window Button --> You will see the following output as shown below:-
xml

Note:- You can see the output of xml codes in XAML file.

For More...
  1. How to learn c# program on Notepad easily
  2. How to implement Form based authentication in asp.net application
  3. Web services in asp.net
  4. Data mining concepts in asp.net
  5. How to use session state in asp.net
  6. Real Views concepts in sql server
Download Attached File
Download

0 comments:

Post a Comment

Powered by Blogger.