简体   繁体   English

如何在C#中使用树视图对XML文件进行数据绑定

[英]How to databind a XML file with a treeview in C#

I want to be able to view a XML file in treeview, and the user can drag the node around in treeview, or delete some node, then the change should be automatically reflected in the XML file. 我希望能够在树视图中查看XML文件,并且用户可以在树视图中拖动节点或删除某些节点,然后更改应自动反映在XML文件中。 To give an example of my purpose, think of the Favorites hierachy in IE: 为了说明我的目的,请考虑一下IE中的“收藏夹”层次结构:

The XML file before modification: 修改前的XML文件:

<Folder FolderName="Favorites">
    <Folder FolderName="Google Websites" >
        <Favorite url="www.google.com">Google</Favorite>
    </Folder>
</Folder>

The treeview before modification: 修改前的treeview:

-Favorites    
--Google Websites  
---Google  

The user dragged a node in the treeview, the treeview becomes: 用户在树视图中拖动一个节点,该树视图变为:

-Favorites
--Google
--Google Websites

Then the XML file should be automatically modified to reflect the change in the treeview: 然后应自动修改XML文件以反映树视图中的更改:

<Folder FolderName="Favorites">
    <Favorite url="www.google.com">Google</Favorite>
    <Folder FolderName="Google Websites" >
    </Folder>
</Folder>

Right now I am able to display the XML in treeview, can drag node around in treeview, but I don't know how to make the XML reflect the changes in the treeview. 现在,我能够在树视图中显示XML,可以在树视图中拖动节点,但是我不知道如何使XML反映树视图中的更改。 Anyone can give me some hint about how to do this in C#? 任何人都可以给我一些有关如何在C#中执行此操作的提示? Thanks in Advance. 提前致谢。

Vincent 文森特

You didn't specify the presentation technology that you are trying to employ. 您没有指定要使用的演示技术。

However, if you are using WPF or Silverlight, the answer is covered in depth in the following question: Two-way binding of Xml data to the WPF TreeView . 但是,如果您使用的是WPF或Silverlight,则以下问题将深入讨论答案: Xml数据与WPF TreeView的双向绑定

Since you're using Windows Forms, I would suggest taking a look at my CodeProject article about ForestPad . 由于您使用的是Windows Forms,因此建议您阅读有关ForestPad的CodeProject文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM