Solved

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

Posted on 2014-01-02
4
3,202 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

757 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