Solved

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

Posted on 2014-01-02
4
3,289 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
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39754213
0
 

Author Comment

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

Accepted Solution

by:
Fernando Soto earned 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Exceptions 3 39
System.Speech 2 16
Windows Media Player 2 16
Best resource to learn entity framework 2 8
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now