Silverlight - 使用 DataGrid 读取和写入 XML 文件

发布于 2024-11-18 22:40:57 字数 1789 浏览 2 评论 0原文

我正在尝试将 xml 数据绑定到 Silverlight 数据网格。目前我一直在“玩”Silverlight 的 DevExpress 工具。我不知道使用这些工具与标准方式相比有什么不同,因为我对 Silverlight 相当陌生。

在他们的网站上,我发现了以下示例,用于从一个 XML 文件检索数据。

现在我希望能够将我在数据网格中所做的修改保存到此 xml 文件,并添加和删除也会影响 XML 文件的行。

namespace XMLReadWrite {
public partial class MainPage : UserControl {

    public MainPage() {
        InitializeComponent();

        grid.ItemsSource = GetData();
    }
    XDocument doc = XDocument.Load("Contacts.xml", LoadOptions.None);

    ObservableCollection<Contact> GetData() {



        var items = from item in doc.Descendants("Contacts")
                    select new Contact() {
                        FirstName = item.Element("FirstName").Value,
                        LastName = item.Element("LastName").Value,
                        Company = item.Element("Company").Value,
                        City = item.Element("City").Value
                        //ID = int.Parse(item.Element("ID").Value)
                    };

        ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();
        foreach (Contact contact in items) {
            contacts.Add(contact);
        }
        return contacts;
    }

    private void SaveToXML()
    { }


    private void Save_Button_Click(object sender, RoutedEventArgs e)
    {
        SaveToXML();
    }        
}

public class Contact {
    public int ID {
        get;
        set;
    }
    public string FirstName {
        get;
        set;
    }
    public string LastName {
        get;
        set;
    }
    public string Company {
        get;
        set;
    }

    public string City
    {
        get;
        set;
    }
 }
}

这可能是一项简单的任务,但我'我被困住了,因为我不知道这里需要做什么。 希望有人可以帮助我。

先感谢您!

I'm trying to bind xml-data to a Silverlight data-grid. At the moment I have been "playing" with the DevExpress tools for Silverlight. I don't know how different it is using these tools compared to the standard way, since I'm fairly new to Silverlight.

On their website I've found the following example to retrieve data from one XML-file.

Now I want to be able to save modifications I made within the datagrid to this xml-file and also add and delete rows that will also affect the XML-file.

namespace XMLReadWrite {
public partial class MainPage : UserControl {

    public MainPage() {
        InitializeComponent();

        grid.ItemsSource = GetData();
    }
    XDocument doc = XDocument.Load("Contacts.xml", LoadOptions.None);

    ObservableCollection<Contact> GetData() {



        var items = from item in doc.Descendants("Contacts")
                    select new Contact() {
                        FirstName = item.Element("FirstName").Value,
                        LastName = item.Element("LastName").Value,
                        Company = item.Element("Company").Value,
                        City = item.Element("City").Value
                        //ID = int.Parse(item.Element("ID").Value)
                    };

        ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();
        foreach (Contact contact in items) {
            contacts.Add(contact);
        }
        return contacts;
    }

    private void SaveToXML()
    { }


    private void Save_Button_Click(object sender, RoutedEventArgs e)
    {
        SaveToXML();
    }        
}

public class Contact {
    public int ID {
        get;
        set;
    }
    public string FirstName {
        get;
        set;
    }
    public string LastName {
        get;
        set;
    }
    public string Company {
        get;
        set;
    }

    public string City
    {
        get;
        set;
    }
 }
}

This is probably an easy task but I'm stuck since I don't have a clue what needs to be done here.
Hopefully someone can help me out.

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜 2024-11-25 22:40:57

您没有提到如何获取 xml 文件,因此有几个选项,一个将其保存到本地存储并从本地存储加载它,这里一篇关于该主题的文章,两个从服务器检索它并将其保存到服务器, 这里一篇关于该主题的文章或三篇文章的组合。就使用 XML 而言,您似乎正在使用 linq to xml 将数据读取到联系人集合中,我建议阅读并浏览这些链接

  1. Linq to Xml 示例
  2. 使用 LINQ to XML在C#中向XML文件添加数据
  3. 使用 LINQ to XML 创建和保存 XML 树

关于 Linq to Xml 的事情重要的是它不是 silverlight 特有的,这些技能也可以很好地转移到服务器编程中。

现在,从数据网格中添加和删除项目的任务,取决于您打算如何构建解决方案,有 MVVM 或简单的代码隐藏,我建议花时间了解 MVVM,它是一种非常适合的设计模式在银光的世界里,从长远来看,它会拯救你的头发。 这里一篇关于使用 MVVM 解决此问题领域的文章。

编辑

  grid.ItemsSource as  ObservableCollection<Contact>();

会将集合归还给您。

You dont mention how you are getting your xml file, so there are few options, one save it to local storage and load it from local storage, heres a article on the subject, two retrieve it from the server and save it to the server, heres an article on the subject or three some combo of either. As far as working with XML, it looks like you are using linq to xml to read the data into a collection of Contacts, I recommend reading and going through these links

  1. Linq to Xml Samples
  2. Using LINQ to XML to Add Data to XML File in C#
  3. Creating and Saving XML tree using LINQ to XML

The thing about Linq to Xml is that its not specific to silverlight, these skills also transfer well to server programming.

Now the task of adding and removing items from the datagrid, well depending on how you deside to architect your solution, theres MVVM or straightforward code-behind, id recommend spending the time to get to know MVVM, its a design pattern that fits very well in the world of silverlight, it will save you hair in the long run. Heres a article on this problem domain using MVVM.

EDIT

  grid.ItemsSource as  ObservableCollection<Contact>();

will give you back the collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文