?
Solved

How to assign value from .xml file to a textbox or label in WPF C#

Posted on 2014-01-02
4
Medium Priority
?
3,660 Views
Last Modified: 2014-01-04
All, I need help in pulling in values from an xml file to textbox/labels on the WPF Form in C#.

Example:
Textbox1 will display the value "Author" from the xml file.
Textbox2 will display the value "Title" from the xml file.
Textbox3 will display the value "Version" from the xml file.
Can anyone help? I manage to get the data loaded into a data grid, but can't seem to pull the required data needed into a label or textbox. This is what I found off the internet, any help will be greatly appreciated.

Current code to load data grid with data from XML file. Need a textbox or label that reads directly from XML file.

WpfApplication.xaml.cs
 
public MainWindow()
        {
            InitializeComponent();
            var xml = XDocument.Load(@"C:\Visual Studio 2010\Projects\WpfApplication\authors.xml").Root;
            
        }

Open in new window


MainWindow.xaml
<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="291" Width="571">


    <Window.Resources>

        <XmlDataProvider x:Key="BookData" 
            Source="C:\Visual Studio 2010\Projects\WpfApplication\authors.xml" XPath="Data"/>

    </Window.Resources>
    <StackPanel>
         <DataGrid 
            ItemsSource="{Binding Path=Elements[Book]}"
            AutoGenerateColumns="False" Height="Auto" 
            Name="dataGrid1" 
            VerticalAlignment="Top" HorizontalAlignment="Stretch">

            <DataGrid.Columns>
                <DataGridTextColumn 
                    Header="Author" 
                    Binding="{Binding Path=Element[Author].Value}"/>
                <DataGridTextColumn 
                    Header="Title" 
                    Binding="{Binding Path=Element[Title].Value}"/>
                <DataGridTextColumn 
                    Header="Version" 
                    Binding="{Binding Path=Element[Version].Value}" />
            </DataGrid.Columns>
 
        </DataGrid>  
    </StackPanel>

Open in new window


authors.xml
<?xml version='1.0'?>
<Data>
 <Book>
 <Author>John Doe</Author>
 <Title>Straight Track Demo</Title>
 <Version>1</Version>  
 </Book>
</Data>

Open in new window

0
Comment
Question by:TeknikDev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:TeknikDev
ID: 39755510
Thanks but doesn't really help for what im trying to do.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39755621
Hi TeknikDev;

I have modified your code so that it works with both the DataGrid and individual TextBox's. Also the line of code in the constructor is not needed.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="291" Width="571">    
    <Window.Resources>
        <XmlDataProvider 
            x:Key="BookData" 
            Source="C:\Visual Studio 2010\Projects\WpfApplication\authors.xml" 
            XPath="/Data"/>
    </Window.Resources>
    <StackPanel DataContext="{StaticResource BookData}">
        <DataGrid 
            ItemsSource="{Binding Source={StaticResource BookData}, XPath=Book}"
            AutoGenerateColumns="False" Height="Auto" 
            Name="dataGrid1" 
            VerticalAlignment="Top" HorizontalAlignment="Stretch">
            <DataGrid.Columns>
                <DataGridTextColumn 
                    Header="Author"
                    Binding="{Binding XPath=Author}"/>
                <DataGridTextColumn 
                    Header="Title" 
                    Binding="{Binding XPath=Title}"/>
                <DataGridTextColumn 
                    Header="Version" 
                    Binding="{Binding XPath=Version}" />
            </DataGrid.Columns>
        </DataGrid>
        <TextBox x:Name="Textbox1" Text="{Binding XPath=Book/Author}" />
        <TextBox x:Name="Textbox2" Text="{Binding XPath=Book/Title}" />
        <TextBox x:Name="Textbox3" Text="{Binding XPath=Book/Version}" />
    </StackPanel>    
</Window>

Open in new window

0
 

Author Comment

by:TeknikDev
ID: 39756371
Wow thanks!!! That worked fine!

Now I have to do a search on how to write to the xml file for updates!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question