English 中文(简体)
DataGrid Loading/Saving XML
原标题:DataGrid Loading/Saving XML

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

我如何从世界森林论坛获得一个数据集,以拯救/装载原始的XML档案,并有能力展示/利用其他控制? 申请将只靠边工作,以便我赢得车库。 简单地说,是开放的、编辑的,并节省XML的数据。

My previous projects with WinForms involved creating a DataSet (xsd file) and a DataTable, bound it to a DataGridView. Then add new items with by calling "AddDataTableRow()". Save/Read XML files by "ReadXML", "WriteXML".

我向世界森林论坛提供新的帮助,将非常感谢。

最佳回答

一种非常直截了当的方法是,简单地界定数据收集类别(包含数据类别实例的可观察的收集),标明这些类别为可序列的类别,并使用XmlSerializer进行序列化/升级。

A sample data class:

[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));
        }
    }
}

and the data collection class:

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

并且把主妇dow与建筑商的代化法和纽特州点击手的序列化法结合起来:

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);
        }
    }
}
问题回答

暂无回答




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签