Solved

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

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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

717 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