DataGrid加载/保存XML

时间:2012-01-14 16:04:43

标签: wpf xml vb.net visual-studio datagrid

我有一个相当直截了当的问题。

如何在WPF中获取DataGrid以保存/加载原始XML文件,并能够使用其他控件显示/编辑它们?该应用程序只能脱机工作,所以我不需要SQL。只需打开,编辑并将数据保存到XML即可。

我之前使用WinForms的项目涉及创建DataSet(xsd文件)和DataTable,将其绑定到DataGridView。然后通过调用“AddDataTableRow()”添加新项目。通过“ReadXML”,“WriteXML”保存/读取XML文件。

我对WPF不熟悉所以任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:1)

一种非常直接的方法是简单地定义数据集合类(包含实现INotifyPropertyChanged的数据类实例的ObservableCollection),将这些类标记为可序列化并使用XmlSerializer进行序列化/反序列化。

示例数据类:

[Serializable]
public class Person : INotifyPropertyChanged
{
    private string firstName;
    private string lastName;

    public event PropertyChangedEventHandler PropertyChanged;

    public string FirstName
    {
        get { return firstName; }
        set
        {
            firstName = value;
            NotifyPropertyChanged("FirstName");
        }
    }

    public string LastName
    {
        get { return lastName; }
        set
        {
            lastName = value;
            NotifyPropertyChanged("LastName");
        }
    }

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

和数据收集类:

[Serializable]
public class PersonCollection : ObservableCollection<Person>
{
}

一些XAML:

<Window x:Class="DataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataGridTest"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:PersonCollection x:Key="PersonCollection"/>
        <CollectionViewSource x:Key="PersonCollectionViewSource" Source="{StaticResource PersonCollection}"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <DataGrid Name="dataGrid" Margin="2" ItemsSource="{Binding Source={StaticResource PersonCollectionViewSource}}"/>
        <StackPanel Grid.Row="1" Margin="2" Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Content="Save" Click="Button_Click"/>
        </StackPanel>
    </Grid>
</Window>

使用构造函数中的deseralization代码和按钮单击处理程序中的序列化代码类MainWindow:

public partial class MainWindow : Window
{
    private PersonCollection persons;

    public MainWindow()
    {
        InitializeComponent();

        persons = (PersonCollection)Resources["PersonCollection"];

        XmlSerializer serializer = new XmlSerializer(typeof(PersonCollection));

        using (FileStream stream = new FileStream("Persons.xml", FileMode.Open))
        {
            IEnumerable<Person> personData = (IEnumerable<Person>)serializer.Deserialize(stream);

            foreach (Person p in personData)
            {
                persons.Add(p);
            }
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(PersonCollection));

        using (FileStream stream = new FileStream("Persons.xml", FileMode.Create))
        {
            serializer.Serialize(stream, persons);
        }
    }
}

答案 1 :(得分:0)

使用Linq和XElement而不是Xdocument和数据集。

http://msdn.microsoft.com/en-us/library/bb669141.aspx