保存/同步文件中已更改的XML内容

时间:2013-01-13 20:02:03

标签: c# wpf xaml

我使用以下代码在ListBox中显示XML文件的内容,并在两个TextBox中显示所选内容。

<?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>

用户可以更改TextBox es中的文字,这也会与ListBox的内容同步。但是如何在XML文件中保存/同步更改?

2 个答案:

答案 0 :(得分:1)

在eventhandler(buttonClick或windowClose)中,您可以访问资源:

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

和提供者有一个Document属性。我不确定你是否可以覆盖,但是:

provider.Document.Save(newFileName);

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

应该有效。

答案 1 :(得分:0)

符合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.

thisthis也可以为您提供帮助。