wewa
wewa

Reputation: 1678

Save/Synchronize changed XML content in file

I am using following code to display the content of an XML file in a ListBox and to display the selected content in two TextBoxes.

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="XML_View_Edit.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Width="500"
    Height="200">
    <Window.Resources>
        <XmlDataProvider x:Key="InventoryData"
                         XPath="Inventory/Books"
                         Source="Data.xml"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition
                Height="100" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <ListBox
            Grid.Row="0"
            Name="listBox1" >
            <ListBox.ItemsSource>
                <Binding Source="{StaticResource InventoryData}" XPath="Book"/>
            </ListBox.ItemsSource>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text = "{Binding XPath=Title}" />
                        <TextBlock Text= " - " />
                        <TextBlock Text = "{Binding XPath=Summary}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Grid
            Grid.Row="1"
             DataContext="{Binding ElementName=listBox1, Path=SelectedItem}">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition
                    Width="70" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Label
                Content="Title"
                Grid.Row="0"
                Grid.Column="0"
                Name="label_title" />
            <Label
                Content="Summary"
                Grid.Row="1"
                Grid.Column="0"
                Name="label_summary" />
            <TextBox
                Grid.Column="1"
                Name="textbox_title"
                Text = "{Binding XPath=Title}"
                Grid.Row="0" />
            <TextBox
                Grid.Column="1"
                Name="textbox_summary"
                Text = "{Binding XPath=Summary}"
                Grid.Row="1" />
        </Grid>
    </Grid>
</Window>

The user can change the text in the TextBoxes and this is also synchronized with the content of the ListBox. But how can I save/synchronize the changes in/with the XML file?

Upvotes: 1

Views: 852

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273179

In an eventhandler (buttonClick or windowClose) you can access the resource:

var provider = Resources["inventoryData"] as XmlDataProvider;

and provider has a Document property. I'm not sure if you can overwrite but something like:

provider.Document.Save(newFileName);

or

provider.Document.Save(provider.Source.ToString());  // existing file name

should work.

Upvotes: 1

Alexander Balte
Alexander Balte

Reputation: 900

Conform to Expert advice on XmlDataProvider usage:

Q: So two way binding utilizing the XmlDataProvider only works between the target and the "in memory" XML? Is there a way without doing my own XML file serialization to get WPF to write out the in memory XML back to the hard disk? A: Correct. You have to do your own file serialization.

Also this and this may help you.

Upvotes: 0

Related Questions