[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3852
  • Last Modified:

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

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
TeknikDev
Asked:
TeknikDev
  • 2
1 Solution
 
TeknikDevAuthor Commented:
Thanks but doesn't really help for what im trying to do.
0
 
Fernando SotoCommented:
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
 
TeknikDevAuthor Commented:
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 SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now