题目:
[quote]第三章实践作业
1.编写一个窗口应用程序,在窗口中有一个Tree控件和一个“浏览”按钮。单击“浏览”按钮,可以打开一个“打开文件”对话框,选择一个XML文件,在Tree中显示XML所对应的树形结构。
2.扩展创新功能(可选的附加功能,属加分项目):对树结构的处理上,例如添加、删除节点。能保存到原XML文件中。
作业要求:
窗口应用程序
功能:
单击按钮——弹出文件选择框——选取XML文件——显示XML文件的树形结构
会用到的控件:Tree控件。
作业涉及的代码内容:
[list][*]Button以及它的事件响应,调用文件选择对话框。
[*]OpenFileDialog类,以及其中部分方法,属性。(主要使用该类中获取文件地址相关的一些属性和方法)
[*]XmlNode和XmlDocument类(用DOM浏览XML)
[*]TreeView类:将DOM中的节点传递到TreeView节点中,实现方法因人而异,像数据结构中的访问树节点,也可以用递归实现。一种递归函数实现方法是:将XmlNode和TreeNodeCollection对象作为参数[/list][/quote]
开发环境:
Visual Studio 2008 Beta 2 / .NET Framework 3.5 Beta 2
.NET Framework最低必需版本:3.5
===============================================================
这次的作业要求也很straight forward。最核心的部分应该就在于XML树形结构的遍历了吧。
这里我采用了递归的深度优先搜索来遍历XML文件。
遍历部分的代码如下,XMLTraverse():
可以看到,这里只定义了遍历一个XML树形结构算法的骨架,却没有把具体要遍历来做什么(action)给写死在里面。
用到了.NET Framework标准库里的一个delegate类型Action<T1, T2>和自定义的一个delegate类型MoveNext<T>,它们的signature分别如下:
Action<T1, T2>指向返回类型为void的方法,用于指定遍历时的动作;MoveNext<T>指向返回类型为T的方法,指定如何将当前集合移动要下一个集合。本来应该用IEnumerable之类的接口更好的,但这里没想出什么好办法来实现,罢了。
(这里有个很明显的死循环bug:如果moveNext参数是null的话,这函数基本上就要死循环了……不过也要看action里到底怎么用。所以就先这么放着了。以后想清楚了再改)
应用这个遍历函数的是PopulateXmlTreeView():
在PopulateXmlTreeView()方法里,指定了XMLTraverse()该具体在遍历时做些什么——这里是将XML节点的信息加入一个TreeView中。注意到这里也应用到了Lambda Expression及相关的类型推断,这两项都是C# 3.0新增的利器。
核心逻辑解决了,开始做界面。打开Visual Studio 2008,新建project,并在designer里拖出界面,如下:
[img]http://bestimmung.iblog.com/unlimit/resserver.php?blogId=222303&resource=shot.jpg&mode=medium[/img]
[url=http://bestimmung.iblog.com/get/222303/shot.jpg]原图链接[/url]
然后把相应的实现代码写出来:
XMLTreeViewForm.cs:
为了实现编辑功能,还另外加了一个非常简单的Form来做文本插入,
InsertElementForm.cs:
到此,实现的功能有(以下内容来自交作业时的readme):
[quote].NET程序设计 第三章上机作业
==============================================
Revision 1
界面如rev_screenshot.jpg所示。左上角是作业所要求的TreeView,用于显示XML文件的树形结构。左下角的ListView用于显示TreeView中当前选定节点中的Attribute列表,不可选中。右侧的TextBox用于显示TreeView中当前选定节点的整个内容(包括其子节点的内容),可以用于编辑当前节点的内容。
要编辑当前节点内容,需先确保在左侧的TreeView中选定了节点,然后在右侧的TextBox中修改内容,并点击“Commit”按钮(工具条中左起第三个),或选用菜单->Edit->Commit Edit
要插入新节点,需先确保在左侧的TreeView中选定了节点,然后点击“Insert”按钮(工具条中左起第四个)或选用菜单->Insert Element->...,在弹出的对话框中输入插入的内容,按对话框右下角的“Insert”按钮确定。注意:无法在根节点的前后插入新节点,也无法在文字节点或注释等非Element节点下添加子节点。
要删除一个节点,需先确保在左侧的TreeView中选定了节点,并点击“Delete”按钮(工具条中左起第五个)或选用菜单->Edit->Delete Element。
==============================================
Revision 0
界面如screenshot.jpg所示。左上角是作业所要求的TreeView,用于显示XML文件的树形结构。左下角的ListView用于显示TreeView中当前选定节点中的Attribute列表。右侧的TextBox用于显示TreeView中当前选定节点的整个内容(包括其子节点的内容)。
已完成的功能有:显示一个XML文件(树形、文字),删除节点,保存修改(到原XML文件或到新文件)。
待完成的功能有:编辑、添加节点。(代码框架已完成,但两处实现细节尚未编码,于是留空)
TreeView、ListView与TextBox之间的分隔可拖动。工具条也可拖动挂靠到上、左、右三侧(禁用了下侧)。
注意:该代码使用了.NET Framework 3.0或以上才支持的delegate Action<T1, T2>,因此无法在.NET Framework 2.0上编译运行。[/quote]
rev_screenshot.jpg:
[img]http://bestimmung.iblog.com/unlimit/resserver.php?blogId=222303&resource=rev_screenshot.JPG&mode=medium[/img]
[url=http://bestimmung.iblog.com/get/222303/rev_screenshot.JPG]原图链接[/url]
screenshot.jpg:
[img]http://bestimmung.iblog.com/unlimit/resserver.php?blogId=222303&resource=262264-screenshot.JPG&mode=medium[/img]
[url=http://bestimmung.iblog.com/get/222303/262264-screenshot.JPG]原图链接[/url]
上面两个Form的designer生成代码分别如下:
XMLTreeView.designer.cs:
InsertElementForm.designer.cs:
这些designer生成的代码要是手写的话肯定痛苦死了……呼。
===============================================================
All right,大功告成 ^ ^
如果还有什么药补充的话,应该就是:为了偷懒,我没有直接用DOM去操纵XML的编辑,而是通过.NET Framework的一个扩展——XPathNavigator来帮忙。这样效率虽说不太高,不过节省了很多手工的调用……诶总之算不上什么好办法就是了 =_=||
[quote]第三章实践作业
1.编写一个窗口应用程序,在窗口中有一个Tree控件和一个“浏览”按钮。单击“浏览”按钮,可以打开一个“打开文件”对话框,选择一个XML文件,在Tree中显示XML所对应的树形结构。
2.扩展创新功能(可选的附加功能,属加分项目):对树结构的处理上,例如添加、删除节点。能保存到原XML文件中。
作业要求:
窗口应用程序
功能:
单击按钮——弹出文件选择框——选取XML文件——显示XML文件的树形结构
会用到的控件:Tree控件。
作业涉及的代码内容:
[list][*]Button以及它的事件响应,调用文件选择对话框。
[*]OpenFileDialog类,以及其中部分方法,属性。(主要使用该类中获取文件地址相关的一些属性和方法)
[*]XmlNode和XmlDocument类(用DOM浏览XML)
[*]TreeView类:将DOM中的节点传递到TreeView节点中,实现方法因人而异,像数据结构中的访问树节点,也可以用递归实现。一种递归函数实现方法是:将XmlNode和TreeNodeCollection对象作为参数[/list][/quote]
开发环境:
Visual Studio 2008 Beta 2 / .NET Framework 3.5 Beta 2
.NET Framework最低必需版本:3.5
===============================================================
这次的作业要求也很straight forward。最核心的部分应该就在于XML树形结构的遍历了吧。
这里我采用了递归的深度优先搜索来遍历XML文件。
遍历部分的代码如下,XMLTraverse():
public void XMLTraverse<T>( XmlNode root, T obj, Action<XmlNode, T> action, MoveNext<T> moveNext ) {
// do something else with currentNode here
if ( action != null ) {
action( root, obj );
}
// recurse predicate
if ( root.NodeType == XmlNodeType.Element ) {
// traverse direct children of current node
XmlNodeList list = root.ChildNodes;
foreach ( XmlNode node in list ) {
T nextobj;
if ( moveNext != null ) {
nextobj = moveNext( obj );
} else {
nextobj = obj;
}
XMLTraverse( node, nextobj, action, moveNext );
}
}
}
可以看到,这里只定义了遍历一个XML树形结构算法的骨架,却没有把具体要遍历来做什么(action)给写死在里面。
用到了.NET Framework标准库里的一个delegate类型Action<T1, T2>和自定义的一个delegate类型MoveNext<T>,它们的signature分别如下:
public delegate void Action<T1, T2>( T1 t1, T2 t2 );
public delegate T MoveNext<T>( T collection );
Action<T1, T2>指向返回类型为void的方法,用于指定遍历时的动作;MoveNext<T>指向返回类型为T的方法,指定如何将当前集合移动要下一个集合。本来应该用IEnumerable之类的接口更好的,但这里没想出什么好办法来实现,罢了。
(这里有个很明显的死循环bug:如果moveNext参数是null的话,这函数基本上就要死循环了……不过也要看action里到底怎么用。所以就先这么放着了。以后想清楚了再改)
应用这个遍历函数的是PopulateXmlTreeView():
private void PopulateXmlTreeView( XmlNode node, TreeNodeCollection tnodes ) {
this.xmlTreeView.BeginUpdate( );
// traverse an XML tree and fill in the xmlTreeView
XMLTraverse(
node,
tnodes,
( xmlnode, treeNodeCol ) => {
if ( xmlnode.NodeType == XmlNodeType.Element ) {
// add the element to xmlTreeView
TreeNode n = new TreeNode( xmlnode.Name );
n.Tag = xmlnode; // tag the TreeNode with current xml node
treeNodeCol.Add( n );
} else {
TreeNode n = new TreeNode( xmlnode.Value.Trim( ) );
n.Tag = xmlnode; // tag the TreeNode with current xml node
treeNodeCol.Add( n );
}
},
collection => collection[ collection.Count - 1 ].Nodes );
this.xmlTreeView.EndUpdate( );
}
在PopulateXmlTreeView()方法里,指定了XMLTraverse()该具体在遍历时做些什么——这里是将XML节点的信息加入一个TreeView中。注意到这里也应用到了Lambda Expression及相关的类型推断,这两项都是C# 3.0新增的利器。
核心逻辑解决了,开始做界面。打开Visual Studio 2008,新建project,并在designer里拖出界面,如下:
[img]http://bestimmung.iblog.com/unlimit/resserver.php?blogId=222303&resource=shot.jpg&mode=medium[/img]
[url=http://bestimmung.iblog.com/get/222303/shot.jpg]原图链接[/url]
然后把相应的实现代码写出来:
XMLTreeViewForm.cs:
using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
namespace XMLTreeView
{
#region Custom Delegate
public delegate T MoveNext<T>( T collection );
#endregion
public partial class XMLTreeViewForm : Form
{
public XMLTreeViewForm( ) {
// Required designer method
InitializeComponent( );
// custom initialize
InitializaListView( );
dlgInsertElement = new InsertElementForm( );
}
~XMLTreeViewForm( ) {
this.dlgInsertElement.Dispose( );
this.Dispose( false );
}
#region initialization
private void InitializaListView( ) {
// Initialize column headers
ColumnHeader colHeader = null;
// First header - filename
colHeader = new ColumnHeader( );
colHeader.Text = "Attribute";
colHeader.Width = -2;
this.xmlAttributeListView.Columns.Add( colHeader );
// Second header - file size in bytes
colHeader = new ColumnHeader( );
colHeader.Text = "Value";
colHeader.Width = -2;
//colHeader.TextAlign = HorizontalAlignment.Right;
this.xmlAttributeListView.Columns.Add( colHeader );
}
#endregion
#region XML related methods
private void LoadFromXMLFile( string path ) {
// lazy initialization of XmlDocument
if ( doc == null ) {
doc = new XmlDocument( );
}
// load XML doc from specified path
doc.Load( path );
doc.Normalize( );
}
private void SaveToXMLFile( string path ) {
// does nothing if file path is null
if ( doc == null ) {
return;
}
// create settings for XmlWriter
XmlWriterSettings settings = new XmlWriterSettings( );
settings.Indent = true;
settings.OmitXmlDeclaration = false;
settings.NewLineOnAttributes = false;
settings.CloseOutput = true;
// write doc with XmlWriter
using ( XmlWriter writer = XmlWriter.Create( path, settings ) ) {
doc.WriteContentTo( writer );
writer.Flush( );
}
}
/// <summary>
/// Traverse an XML tree from the specified node
/// </summary>
/// <param name="currentNode"></param>
public void XMLTraverse<T>( XmlNode root, T obj, Action<XmlNode, T> action, MoveNext<T> moveNext ) {
// do something else with currentNode here
if ( action != null ) {
action( root, obj );
}
// recurse predicate
if ( root.NodeType == XmlNodeType.Element ) {
// traverse direct children of current node
XmlNodeList list = root.ChildNodes;
foreach ( XmlNode node in list ) {
T nextobj;
if ( moveNext != null ) {
nextobj = moveNext( obj );
} else {
nextobj = obj;
}
XMLTraverse( node, nextobj, action, moveNext );
}
}
}
#endregion
#region xmlTreeView related methods
private void PopulateXmlTreeView( XmlNode node, TreeNodeCollection tnodes ) {
this.xmlTreeView.BeginUpdate( );
// traverse an XML tree and fill in the xmlTreeView
XMLTraverse(
node,
tnodes,
( xmlnode, treeNodeCol ) => {
if ( xmlnode.NodeType == XmlNodeType.Element ) {
// add the element to xmlTreeView
TreeNode n = new TreeNode( xmlnode.Name );
n.Tag = xmlnode; // tag the TreeNode with current xml node
treeNodeCol.Add( n );
} else {
TreeNode n = new TreeNode( xmlnode.Value.Trim( ) );
n.Tag = xmlnode; // tag the TreeNode with current xml node
treeNodeCol.Add( n );
}
},
collection => collection[ collection.Count - 1 ].Nodes );
this.xmlTreeView.EndUpdate( );
}
private void SelectedTreeView( ) {
// get current path
XmlNode xmlnode = ( XmlNode ) this.xmlTreeView.SelectedNode.Tag;
// load xmlAttributeListView with attributes of current node
PopulateXmlAttributeListView( xmlnode );
// load xmlElementTextBox with all contents of current node
PopulateXmlElementTextBox( xmlnode );
}
private enum InsertOptions
{
Before,
After,
Prepend,
Append,
Replace
}
private void InsertNode( TreeNode node, string xmlSnippet, InsertOptions option ) {
TreeNode parent = node.Parent;
XmlNode xmlnode = node.Tag as XmlNode;
if ( xmlnode != null ) {
try {
XPathNavigator nav = doc.CreateNavigator( );
nav.MoveTo( xmlnode.CreateNavigator( ) );
XmlNode xmlparentnode = xmlnode.ParentNode;
switch ( option ) {
case InsertOptions.Before:
nav.InsertBefore( xmlSnippet );
break;
case InsertOptions.After:
nav.InsertAfter( xmlSnippet );
break;
case InsertOptions.Prepend:
nav.PrependChild( xmlSnippet );
break;
case InsertOptions.Append:
nav.AppendChild( xmlSnippet );
break;
case InsertOptions.Replace:
nav.ReplaceSelf( xmlSnippet );
break;
}
doc.Normalize( ); // TODO - may remove this call
TreeNode dummyroot = new TreeNode( );
if ( xmlparentnode != null && xmlparentnode.NodeType != XmlNodeType.Document ) {
PopulateXmlTreeView( xmlparentnode, dummyroot.Nodes );
parent.Nodes.Clear( );
TreeNode[ ] dummys = new TreeNode[ dummyroot.FirstNode.Nodes.Count ];
dummyroot.FirstNode.Nodes.CopyTo( dummys, 0 );
parent.Nodes.AddRange( dummys );
this.xmlTreeView.SelectedNode = parent;
} else {
this.xmlTreeView.Nodes.Clear( );
PopulateXmlTreeView( doc.DocumentElement, this.xmlTreeView.Nodes );
this.xmlTreeView.SelectedNode = this.xmlTreeView.Nodes[ 0 ];
}
SelectedTreeView( );
} catch ( Exception ) {
MessageBox.Show( string.Format(
"The XML snippet is invalid: {0}{1}", Environment.NewLine, xmlSnippet ),
"Error" );
}
}
}
private void DeleteSelectedElement( ) {
TreeNode selected = this.xmlTreeView.SelectedNode;
XmlNode xmlnode = selected.Tag as XmlNode;
this.xmlTreeView.SelectedNode = selected.Parent;
if ( xmlnode != null ) {
xmlnode.ParentNode.RemoveChild( xmlnode );
selected.Remove( );
}
SelectedTreeView( );
}
#endregion
#region xmlElementTextBox related methods
private void PopulateXmlElementTextBox( XmlNode node ) {
// save original text as backup
string originalText = this.xmlElementTextBox.Text;
try {
StringBuilder sb = new StringBuilder( );
// create settings for XmlWriter
XmlWriterSettings settings = new XmlWriterSettings( );
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.NewLineOnAttributes = true;
settings.CloseOutput = false;
settings.ConformanceLevel = ConformanceLevel.Fragment;
// write element with XmlWriter
using ( XmlWriter writer = XmlWriter.Create( sb, settings ) ) {
node.WriteTo( writer );
writer.Flush( );
}
// set text with new element
this.xmlElementTextBox.Text = sb.ToString( );
} catch ( Exception ) {
// restore original text
this.xmlElementTextBox.Text = originalText;
}
this.xmlElementTextBox.Refresh( );
}
#endregion
#region xmlAttributeListView related methods
private void PopulateXmlAttributeListView( XmlNode node ) {
if ( node == null ) {
return;
}
ListViewItem item = null;
ListViewItem.ListViewSubItem subitem = null;
this.xmlAttributeListView.Items.Clear( );
this.xmlAttributeListView.BeginUpdate( );
XmlAttributeCollection attrs = node.Attributes;
if ( attrs != null ) {
foreach ( XmlAttribute attr in attrs ) {
item = new ListViewItem( );
item.Text = attr.Name;
subitem = new ListViewItem.ListViewSubItem( );
subitem.Text = attr.Value;
item.SubItems.Add( subitem );
this.xmlAttributeListView.Items.Add( item );
}
}
// Automatic adjustment of column width,
// -1 for longest item, -2 for longer of header and longest item
this.xmlAttributeListView.Columns[ 0 ].Width = -2;
this.xmlAttributeListView.Columns[ 1 ].Width = -2;
this.xmlAttributeListView.EndUpdate( );
}
#endregion
#region GUI Event Handlers
private void xmlTreeView_AfterSelect( object sender, EventArgs e ) {
SelectedTreeView( );
}
private void btnOpenFile_Click( object sender, EventArgs e ) {
this.openFileDialog.ShowDialog( );
}
private void openFileDialog_FileOk( object sender, CancelEventArgs e ) {
OpenFileDialog dlg = sender as OpenFileDialog;
if ( dlg != null ) {
// save current filename
this.currentFilePath = dlg.FileName; // dlg.FileName is full path name and is never null
// Clear XMLTreeView before loading a new file
this.xmlTreeView.Nodes.Clear( );
// load new file
LoadFromXMLFile( this.currentFilePath );
doc.Normalize( );
// do updates
PopulateXmlElementTextBox( doc.DocumentElement );
PopulateXmlTreeView( doc.DocumentElement, this.xmlTreeView.Nodes );
if ( this.xmlTreeView.Nodes.Count != 0 ) {
this.xmlTreeView.Nodes[ 0 ].Expand( );
}
// set saveFileDialog's default filename to current filename
this.saveFileDialog.FileName = this.currentFilePath;
}
}
private void tdpbtnSaveFile_Click( object sender, EventArgs e ) {
if ( this.doc == null ) {
return;
}
SaveToXMLFile( this.currentFilePath );
}
private void saveFileDialog_FileOk( object sender, CancelEventArgs e ) {
SaveFileDialog dlg = sender as SaveFileDialog;
if ( dlg != null ) {
this.currentFilePath = dlg.FileName;
SaveToXMLFile( this.currentFilePath );
}
}
private void menuItemOpenFile_Click( object sender, EventArgs e ) {
this.openFileDialog.ShowDialog( );
}
private void menuItemSaveFile_Click( object sender, EventArgs e ) {
SaveToXMLFile( this.currentFilePath );
}
private void menuItemSaveAsFile_Click( object sender, EventArgs e ) {
if ( this.doc == null ) {
return;
}
this.saveFileDialog.ShowDialog( );
}
private void menuItemExit_Click( object sender, EventArgs e ) {
Application.Exit( );
}
private void tbtnDeleteElement_Click( object sender, EventArgs e ) {
DeleteSelectedElement( );
}
// helper, check if specified TreeNode is root
private bool CheckForElement( TreeNode node ) {
XmlNode xmlnode = node.Tag as XmlNode;
if ( xmlnode != null && xmlnode.NodeType != XmlNodeType.Element ) {
MessageBox.Show( "Cannot prepend or append on XML node other than an element.", "Error" );
return false;
}
return true;
}
private void menuItemPrepend_Click( object sender, EventArgs e ) {
if ( !CheckForElement( this.xmlTreeView.SelectedNode ) ) {
return;
}
if ( this.dlgInsertElement.ShowDialog( ) == DialogResult.OK ) {
string contents = this.dlgInsertElement.InsertContent;
InsertNode( this.xmlTreeView.SelectedNode, contents, InsertOptions.Prepend );
}
}
private void menuItemAppend_Click( object sender, EventArgs e ) {
if ( !CheckForElement( this.xmlTreeView.SelectedNode ) ) {
return;
}
if ( this.dlgInsertElement.ShowDialog( ) == DialogResult.OK ) {
string contents = this.dlgInsertElement.InsertContent;
InsertNode( this.xmlTreeView.SelectedNode, contents, InsertOptions.Append );
}
}
// helper, check if specified TreeNode is root
private bool CheckForRoot( TreeNode node ) {
if ( node == this.xmlTreeView.Nodes[ 0 ] ) {
MessageBox.Show( "Cannot insert new XML node before or after the root.", "Error" );
return false;
}
return true;
}
private void menuItemInsertAfter_Click( object sender, EventArgs e ) {
if ( !CheckForRoot( this.xmlTreeView.SelectedNode ) ) {
return;
}
if ( this.dlgInsertElement.ShowDialog( ) == DialogResult.OK ) {
string contents = this.dlgInsertElement.InsertContent;
InsertNode( this.xmlTreeView.SelectedNode, contents, InsertOptions.After );
}
}
private void menuItemInsertBefore_Click( object sender, EventArgs e ) {
if ( !CheckForRoot( this.xmlTreeView.SelectedNode ) ) {
return;
}
if ( this.dlgInsertElement.ShowDialog( ) == DialogResult.OK ) {
string contents = this.dlgInsertElement.InsertContent;
InsertNode( this.xmlTreeView.SelectedNode, contents, InsertOptions.Before );
}
}
private void menuItemEditElement_Click( object sender, EventArgs e ) {
string contents = this.xmlElementTextBox.Text;
InsertNode( this.xmlTreeView.SelectedNode, contents, InsertOptions.Replace );
}
private void menuItemDeleteElement_Click( object sender, EventArgs e ) {
DeleteSelectedElement( );
}
#endregion
#region member field declaration
private XmlDocument doc;
private string currentFilePath;
private InsertElementForm dlgInsertElement;
#endregion
}
}
为了实现编辑功能,还另外加了一个非常简单的Form来做文本插入,
InsertElementForm.cs:
using System;
using System.Windows.Forms;
namespace XMLTreeView
{
public partial class InsertElementForm : Form
{
public event EventHandler ElementOk;
public string InsertContent {
get {
return this.txtContent.Text;
}
}
public InsertElementForm( ) {
InitializeComponent( );
}
private void OnElementOk( EventArgs e ) {
EventHandler handler = this.ElementOk;
if ( handler != null ) {
handler( this, e );
}
}
private void btnOk_Click( object sender, EventArgs e ) {
if ( this.txtContent.Text == null || this.txtContent.Text.Length == 0 ) {
return;
}
OnElementOk( new EventArgs( ) );
}
}
}
到此,实现的功能有(以下内容来自交作业时的readme):
[quote].NET程序设计 第三章上机作业
==============================================
Revision 1
界面如rev_screenshot.jpg所示。左上角是作业所要求的TreeView,用于显示XML文件的树形结构。左下角的ListView用于显示TreeView中当前选定节点中的Attribute列表,不可选中。右侧的TextBox用于显示TreeView中当前选定节点的整个内容(包括其子节点的内容),可以用于编辑当前节点的内容。
要编辑当前节点内容,需先确保在左侧的TreeView中选定了节点,然后在右侧的TextBox中修改内容,并点击“Commit”按钮(工具条中左起第三个),或选用菜单->Edit->Commit Edit
要插入新节点,需先确保在左侧的TreeView中选定了节点,然后点击“Insert”按钮(工具条中左起第四个)或选用菜单->Insert Element->...,在弹出的对话框中输入插入的内容,按对话框右下角的“Insert”按钮确定。注意:无法在根节点的前后插入新节点,也无法在文字节点或注释等非Element节点下添加子节点。
要删除一个节点,需先确保在左侧的TreeView中选定了节点,并点击“Delete”按钮(工具条中左起第五个)或选用菜单->Edit->Delete Element。
==============================================
Revision 0
界面如screenshot.jpg所示。左上角是作业所要求的TreeView,用于显示XML文件的树形结构。左下角的ListView用于显示TreeView中当前选定节点中的Attribute列表。右侧的TextBox用于显示TreeView中当前选定节点的整个内容(包括其子节点的内容)。
已完成的功能有:显示一个XML文件(树形、文字),删除节点,保存修改(到原XML文件或到新文件)。
待完成的功能有:编辑、添加节点。(代码框架已完成,但两处实现细节尚未编码,于是留空)
TreeView、ListView与TextBox之间的分隔可拖动。工具条也可拖动挂靠到上、左、右三侧(禁用了下侧)。
注意:该代码使用了.NET Framework 3.0或以上才支持的delegate Action<T1, T2>,因此无法在.NET Framework 2.0上编译运行。[/quote]
rev_screenshot.jpg:
[img]http://bestimmung.iblog.com/unlimit/resserver.php?blogId=222303&resource=rev_screenshot.JPG&mode=medium[/img]
[url=http://bestimmung.iblog.com/get/222303/rev_screenshot.JPG]原图链接[/url]
screenshot.jpg:
[img]http://bestimmung.iblog.com/unlimit/resserver.php?blogId=222303&resource=262264-screenshot.JPG&mode=medium[/img]
[url=http://bestimmung.iblog.com/get/222303/262264-screenshot.JPG]原图链接[/url]
上面两个Form的designer生成代码分别如下:
XMLTreeView.designer.cs:
namespace XMLTreeView
{
partial class XMLTreeViewForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose( bool disposing ) {
if ( disposing && ( components != null ) ) {
components.Dispose();
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent( ) {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager( typeof( XMLTreeViewForm ) );
this.panel2 = new System.Windows.Forms.Panel( );
this.splitContainerBig = new System.Windows.Forms.SplitContainer( );
this.splitContainerSub = new System.Windows.Forms.SplitContainer( );
this.xmlTreeView = new System.Windows.Forms.TreeView( );
this.xmlAttributeListView = new System.Windows.Forms.ListView( );
this.xmlElementTextBox = new System.Windows.Forms.TextBox( );
this.openFileDialog = new System.Windows.Forms.OpenFileDialog( );
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog( );
this.toolStripContainer1 = new System.Windows.Forms.ToolStripContainer( );
this.menuStrip1 = new System.Windows.Forms.MenuStrip( );
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem( );
this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem( );
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem( );
this.saveAsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem( );
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator( );
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem( );
this.editToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem( );
this.editElementToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem( );
this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator( );
this.insertElementToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem( );
this.mnitmBeforeSelected = new System.Windows.Forms.ToolStripMenuItem( );
this.mnitmAfterSelected = new System.Windows.Forms.ToolStripMenuItem( );
this.mnitmPrepend = new System.Windows.Forms.ToolStripMenuItem( );
this.mnitmAppend = new System.Windows.Forms.ToolStripMenuItem( );
this.rToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem( );
this.toolStrip1 = new System.Windows.Forms.ToolStrip( );
this.btnOpenFile = new System.Windows.Forms.ToolStripButton( );
this.tsbtnSaveFile = new System.Windows.Forms.ToolStripSplitButton( );
this.saveToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem( );
this.saveAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem( );
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator( );
this.tbtnEditElement = new System.Windows.Forms.ToolStripButton( );
this.tbtnInsertElement = new System.Windows.Forms.ToolStripDropDownButton( );
this.tmnitmBeforeSelected = new System.Windows.Forms.ToolStripMenuItem( );
this.tmnitmAfterSelected = new System.Windows.Forms.ToolStripMenuItem( );
this.tmnitmPrepend = new System.Windows.Forms.ToolStripMenuItem( );
this.tmnitmAppend = new System.Windows.Forms.ToolStripMenuItem( );
this.tbtnDeleteElement = new System.Windows.Forms.ToolStripButton( );
this.panel2.SuspendLayout( );
this.splitContainerBig.Panel1.SuspendLayout( );
this.splitContainerBig.Panel2.SuspendLayout( );
this.splitContainerBig.SuspendLayout( );
this.splitContainerSub.Panel1.SuspendLayout( );
this.splitContainerSub.Panel2.SuspendLayout( );
this.splitContainerSub.SuspendLayout( );
this.toolStripContainer1.ContentPanel.SuspendLayout( );
this.toolStripContainer1.TopToolStripPanel.SuspendLayout( );
this.toolStripContainer1.SuspendLayout( );
this.menuStrip1.SuspendLayout( );
this.toolStrip1.SuspendLayout( );
this.SuspendLayout( );
//
// panel2
//
this.panel2.Controls.Add( this.splitContainerBig );
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point( 0, 0 );
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size( 940, 590 );
this.panel2.TabIndex = 1;
//
// splitContainerBig
//
this.splitContainerBig.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.splitContainerBig.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainerBig.Location = new System.Drawing.Point( 0, 0 );
this.splitContainerBig.Name = "splitContainerBig";
//
// splitContainerBig.Panel1
//
this.splitContainerBig.Panel1.Controls.Add( this.splitContainerSub );
//
// splitContainerBig.Panel2
//
this.splitContainerBig.Panel2.Controls.Add( this.xmlElementTextBox );
this.splitContainerBig.Size = new System.Drawing.Size( 940, 590 );
this.splitContainerBig.SplitterDistance = 249;
this.splitContainerBig.TabIndex = 0;
//
// splitContainerSub
//
this.splitContainerSub.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainerSub.Location = new System.Drawing.Point( 0, 0 );
this.splitContainerSub.Name = "splitContainerSub";
this.splitContainerSub.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainerSub.Panel1
//
this.splitContainerSub.Panel1.Controls.Add( this.xmlTreeView );
//
// splitContainerSub.Panel2
//
this.splitContainerSub.Panel2.Controls.Add( this.xmlAttributeListView );
this.splitContainerSub.Size = new System.Drawing.Size( 245, 586 );
this.splitContainerSub.SplitterDistance = 415;
this.splitContainerSub.TabIndex = 0;
//
// xmlTreeView
//
this.xmlTreeView.Dock = System.Windows.Forms.DockStyle.Fill;
this.xmlTreeView.Location = new System.Drawing.Point( 0, 0 );
this.xmlTreeView.Name = "xmlTreeView";
this.xmlTreeView.Size = new System.Drawing.Size( 245, 415 );
this.xmlTreeView.TabIndex = 0;
this.xmlTreeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler( this.xmlTreeView_AfterSelect );
//
// xmlAttributeListView
//
this.xmlAttributeListView.Dock = System.Windows.Forms.DockStyle.Fill;
this.xmlAttributeListView.GridLines = true;
this.xmlAttributeListView.Location = new System.Drawing.Point( 0, 0 );
this.xmlAttributeListView.Name = "xmlAttributeListView";
this.xmlAttributeListView.Size = new System.Drawing.Size( 245, 167 );
this.xmlAttributeListView.TabIndex = 0;
this.xmlAttributeListView.UseCompatibleStateImageBehavior = false;
this.xmlAttributeListView.View = System.Windows.Forms.View.Details;
//
// xmlElementTextBox
//
this.xmlElementTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.xmlElementTextBox.Location = new System.Drawing.Point( 0, 0 );
this.xmlElementTextBox.Multiline = true;
this.xmlElementTextBox.Name = "xmlElementTextBox";
this.xmlElementTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.xmlElementTextBox.Size = new System.Drawing.Size( 683, 586 );
this.xmlElementTextBox.TabIndex = 0;
this.xmlElementTextBox.WordWrap = false;
//
// openFileDialog
//
this.openFileDialog.DefaultExt = "xml";
this.openFileDialog.Filter = "XML files (*.xml)|*.xml";
this.openFileDialog.Title = "Open XML File";
this.openFileDialog.FileOk += new System.ComponentModel.CancelEventHandler( this.openFileDialog_FileOk );
//
// saveFileDialog
//
this.saveFileDialog.DefaultExt = "xml";
this.saveFileDialog.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
this.saveFileDialog.Title = "Save File";
this.saveFileDialog.FileOk += new System.ComponentModel.CancelEventHandler( this.saveFileDialog_FileOk );
//
// toolStripContainer1
//
this.toolStripContainer1.BottomToolStripPanelVisible = false;
//
// toolStripContainer1.ContentPanel
//
this.toolStripContainer1.ContentPanel.Controls.Add( this.panel2 );
this.toolStripContainer1.ContentPanel.Size = new System.Drawing.Size( 940, 590 );
this.toolStripContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.toolStripContainer1.Location = new System.Drawing.Point( 0, 0 );
this.toolStripContainer1.Name = "toolStripContainer1";
this.toolStripContainer1.Size = new System.Drawing.Size( 940, 639 );
this.toolStripContainer1.TabIndex = 2;
this.toolStripContainer1.Text = "toolStripContainer1";
//
// toolStripContainer1.TopToolStripPanel
//
this.toolStripContainer1.TopToolStripPanel.Controls.Add( this.menuStrip1 );
this.toolStripContainer1.TopToolStripPanel.Controls.Add( this.toolStrip1 );
//
// menuStrip1
//
this.menuStrip1.Dock = System.Windows.Forms.DockStyle.None;
this.menuStrip1.Items.AddRange( new System.Windows.Forms.ToolStripItem[ ] {
this.fileToolStripMenuItem,
this.editToolStripMenuItem} );
this.menuStrip1.Location = new System.Drawing.Point( 0, 0 );
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size( 940, 24 );
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange( new System.Windows.Forms.ToolStripItem[ ] {
this.openToolStripMenuItem,
this.saveToolStripMenuItem,
this.saveAsToolStripMenuItem1,
this.toolStripSeparator1,
this.exitToolStripMenuItem} );
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size( 41, 20 );
this.fileToolStripMenuItem.Text = "File";
//
// openToolStripMenuItem
//
this.openToolStripMenuItem.Name = "openToolStripMenuItem";
this.openToolStripMenuItem.Size = new System.Drawing.Size( 130, 22 );
this.openToolStripMenuItem.Text = "&Open";
this.openToolStripMenuItem.Click += new System.EventHandler( this.menuItemOpenFile_Click );
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size( 130, 22 );
this.saveToolStripMenuItem.Text = "&Save";
this.saveToolStripMenuItem.Click += new System.EventHandler( this.menuItemSaveFile_Click );
//
// saveAsToolStripMenuItem1
//
this.saveAsToolStripMenuItem1.Name = "saveAsToolStripMenuItem1";
this.saveAsToolStripMenuItem1.Size = new System.Drawing.Size( 130, 22 );
this.saveAsToolStripMenuItem1.Text = "S&ave As...";
this.saveAsToolStripMenuItem1.Click += new System.EventHandler( this.menuItemSaveAsFile_Click );
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size( 127, 6 );
//
// exitToolStripMenuItem
//
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size( 130, 22 );
this.exitToolStripMenuItem.Text = "E&xit";
this.exitToolStripMenuItem.Click += new System.EventHandler( this.menuItemExit_Click );
//
// editToolStripMenuItem
//
this.editToolStripMenuItem.DropDownItems.AddRange( new System.Windows.Forms.ToolStripItem[ ] {
this.editElementToolStripMenuItem,
this.toolStripSeparator3,
this.insertElementToolStripMenuItem,
this.rToolStripMenuItem} );
this.editToolStripMenuItem.Name = "editToolStripMenuItem";
this.editToolStripMenuItem.Size = new System.Drawing.Size( 41, 20 );
this.editToolStripMenuItem.Text = "&Edit";
//
// editElementToolStripMenuItem
//
this.editElementToolStripMenuItem.Name = "editElementToolStripMenuItem";
this.editElementToolStripMenuItem.Size = new System.Drawing.Size( 154, 22 );
this.editElementToolStripMenuItem.Text = "&Commit Edit";
this.editElementToolStripMenuItem.Click += new System.EventHandler( this.menuItemEditElement_Click );
//
// toolStripSeparator3
//
this.toolStripSeparator3.Name = "toolStripSeparator3";
this.toolStripSeparator3.Size = new System.Drawing.Size( 151, 6 );
//
// insertElementToolStripMenuItem
//
this.insertElementToolStripMenuItem.DropDownItems.AddRange( new System.Windows.Forms.ToolStripItem[ ] {
this.mnitmBeforeSelected,
this.mnitmAfterSelected,
this.mnitmPrepend,
this.mnitmAppend} );
this.insertElementToolStripMenuItem.Name = "insertElementToolStripMenuItem";
this.insertElementToolStripMenuItem.Size = new System.Drawing.Size( 154, 22 );
this.insertElementToolStripMenuItem.Text = "&Insert Element";
//
// mnitmBeforeSelected
//
this.mnitmBeforeSelected.Name = "mnitmBeforeSelected";
this.mnitmBeforeSelected.Size = new System.Drawing.Size( 184, 22 );
this.mnitmBeforeSelected.Text = "&Before Selected";
this.mnitmBeforeSelected.Click += new System.EventHandler( this.menuItemInsertBefore_Click );
//
// mnitmAfterSelected
//
this.mnitmAfterSelected.Name = "mnitmAfterSelected";
this.mnitmAfterSelected.Size = new System.Drawing.Size( 184, 22 );
this.mnitmAfterSelected.Text = "&After Selected";
this.mnitmAfterSelected.Click += new System.EventHandler( this.menuItemInsertAfter_Click );
//
// mnitmPrepend
//
this.mnitmPrepend.Name = "mnitmPrepend";
this.mnitmPrepend.Size = new System.Drawing.Size( 184, 22 );
this.mnitmPrepend.Text = "&Prepend to Selected";
this.mnitmPrepend.Click += new System.EventHandler( this.menuItemPrepend_Click );
//
// mnitmAppend
//
this.mnitmAppend.Name = "mnitmAppend";
this.mnitmAppend.Size = new System.Drawing.Size( 184, 22 );
this.mnitmAppend.Text = "App&end to Selected";
this.mnitmAppend.Click += new System.EventHandler( this.menuItemAppend_Click );
//
// rToolStripMenuItem
//
this.rToolStripMenuItem.Name = "rToolStripMenuItem";
this.rToolStripMenuItem.Size = new System.Drawing.Size( 154, 22 );
this.rToolStripMenuItem.Text = "&Delete Element";
this.rToolStripMenuItem.Click += new System.EventHandler( this.menuItemDeleteElement_Click );
//
// toolStrip1
//
this.toolStrip1.Dock = System.Windows.Forms.DockStyle.None;
this.toolStrip1.Items.AddRange( new System.Windows.Forms.ToolStripItem[ ] {
this.btnOpenFile,
this.tsbtnSaveFile,
this.toolStripSeparator2,
this.tbtnEditElement,
this.tbtnInsertElement,
this.tbtnDeleteElement} );
this.toolStrip1.Location = new System.Drawing.Point( 3, 24 );
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size( 148, 25 );
this.toolStrip1.TabIndex = 1;
//
// btnOpenFile
//
this.btnOpenFile.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.btnOpenFile.Image = ( ( System.Drawing.Image ) ( resources.GetObject( "btnOpenFile.Image" ) ) );
this.btnOpenFile.ImageTransparentColor = System.Drawing.Color.Magenta;
this.btnOpenFile.Name = "btnOpenFile";
this.btnOpenFile.Size = new System.Drawing.Size( 23, 22 );
this.btnOpenFile.Text = "Open File";
this.btnOpenFile.Click += new System.EventHandler( this.btnOpenFile_Click );
//
// tsbtnSaveFile
//
this.tsbtnSaveFile.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.tsbtnSaveFile.DropDownItems.AddRange( new System.Windows.Forms.ToolStripItem[ ] {
this.saveToolStripMenuItem1,
this.saveAsToolStripMenuItem} );
this.tsbtnSaveFile.Image = ( ( System.Drawing.Image ) ( resources.GetObject( "tsbtnSaveFile.Image" ) ) );
this.tsbtnSaveFile.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tsbtnSaveFile.Name = "tsbtnSaveFile";
this.tsbtnSaveFile.Size = new System.Drawing.Size( 32, 22 );
this.tsbtnSaveFile.Text = "Save File";
this.tsbtnSaveFile.Click += new System.EventHandler( this.tdpbtnSaveFile_Click );
//
// saveToolStripMenuItem1
//
this.saveToolStripMenuItem1.Name = "saveToolStripMenuItem1";
this.saveToolStripMenuItem1.Size = new System.Drawing.Size( 130, 22 );
this.saveToolStripMenuItem1.Text = "Save";
this.saveToolStripMenuItem1.Click += new System.EventHandler( this.menuItemSaveFile_Click );
//
// saveAsToolStripMenuItem
//
this.saveAsToolStripMenuItem.Name = "saveAsToolStripMenuItem";
this.saveAsToolStripMenuItem.Size = new System.Drawing.Size( 130, 22 );
this.saveAsToolStripMenuItem.Text = "Save As...";
this.saveAsToolStripMenuItem.Click += new System.EventHandler( this.menuItemSaveAsFile_Click );
//
// toolStripSeparator2
//
this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size( 6, 25 );
//
// tbtnEditElement
//
this.tbtnEditElement.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.tbtnEditElement.Image = ( ( System.Drawing.Image ) ( resources.GetObject( "tbtnEditElement.Image" ) ) );
this.tbtnEditElement.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tbtnEditElement.Name = "tbtnEditElement";
this.tbtnEditElement.Size = new System.Drawing.Size( 23, 22 );
this.tbtnEditElement.Text = "Commit Edit to Current Element";
this.tbtnEditElement.Click += new System.EventHandler( this.menuItemEditElement_Click );
//
// tbtnInsertElement
//
this.tbtnInsertElement.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.tbtnInsertElement.DropDownItems.AddRange( new System.Windows.Forms.ToolStripItem[ ] {
this.tmnitmBeforeSelected,
this.tmnitmAfterSelected,
this.tmnitmPrepend,
this.tmnitmAppend} );
this.tbtnInsertElement.Image = ( ( System.Drawing.Image ) ( resources.GetObject( "tbtnInsertElement.Image" ) ) );
this.tbtnInsertElement.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tbtnInsertElement.Name = "tbtnInsertElement";
this.tbtnInsertElement.Size = new System.Drawing.Size( 29, 22 );
this.tbtnInsertElement.Text = "Insert Element";
//
// tmnitmBeforeSelected
//
this.tmnitmBeforeSelected.Name = "tmnitmBeforeSelected";
this.tmnitmBeforeSelected.Size = new System.Drawing.Size( 178, 22 );
this.tmnitmBeforeSelected.Text = "Before Selected";
this.tmnitmBeforeSelected.Click += new System.EventHandler( this.menuItemInsertBefore_Click );
//
// tmnitmAfterSelected
//
this.tmnitmAfterSelected.Name = "tmnitmAfterSelected";
this.tmnitmAfterSelected.Size = new System.Drawing.Size( 178, 22 );
this.tmnitmAfterSelected.Text = "After Selected";
this.tmnitmAfterSelected.Click += new System.EventHandler( this.menuItemInsertAfter_Click );
//
// tmnitmPrepend
//
this.tmnitmPrepend.Name = "tmnitmPrepend";
this.tmnitmPrepend.Size = new System.Drawing.Size( 178, 22 );
this.tmnitmPrepend.Text = "Prepend as a Child";
this.tmnitmPrepend.Click += new System.EventHandler( this.menuItemPrepend_Click );
//
// tmnitmAppend
//
this.tmnitmAppend.Name = "tmnitmAppend";
this.tmnitmAppend.Size = new System.Drawing.Size( 178, 22 );
this.tmnitmAppend.Text = "Append as a Child";
this.tmnitmAppend.Click += new System.EventHandler( this.menuItemAppend_Click );
//
// tbtnDeleteElement
//
this.tbtnDeleteElement.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.tbtnDeleteElement.Image = ( ( System.Drawing.Image ) ( resources.GetObject( "tbtnDeleteElement.Image" ) ) );
this.tbtnDeleteElement.ImageTransparentColor = System.Drawing.Color.Magenta;
this.tbtnDeleteElement.Name = "tbtnDeleteElement";
this.tbtnDeleteElement.Size = new System.Drawing.Size( 23, 22 );
this.tbtnDeleteElement.Text = "Delete Element";
this.tbtnDeleteElement.Click += new System.EventHandler( this.tbtnDeleteElement_Click );
//
// XMLTreeViewForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 12F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size( 940, 639 );
this.Controls.Add( this.toolStripContainer1 );
this.Name = "XMLTreeViewForm";
this.Text = "XML Tree Viewer";
this.panel2.ResumeLayout( false );
this.splitContainerBig.Panel1.ResumeLayout( false );
this.splitContainerBig.Panel2.ResumeLayout( false );
this.splitContainerBig.Panel2.PerformLayout( );
this.splitContainerBig.ResumeLayout( false );
this.splitContainerSub.Panel1.ResumeLayout( false );
this.splitContainerSub.Panel2.ResumeLayout( false );
this.splitContainerSub.ResumeLayout( false );
this.toolStripContainer1.ContentPanel.ResumeLayout( false );
this.toolStripContainer1.TopToolStripPanel.ResumeLayout( false );
this.toolStripContainer1.TopToolStripPanel.PerformLayout( );
this.toolStripContainer1.ResumeLayout( false );
this.toolStripContainer1.PerformLayout( );
this.menuStrip1.ResumeLayout( false );
this.menuStrip1.PerformLayout( );
this.toolStrip1.ResumeLayout( false );
this.toolStrip1.PerformLayout( );
this.ResumeLayout( false );
}
#endregion
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.TreeView xmlTreeView;
private System.Windows.Forms.OpenFileDialog openFileDialog;
private System.Windows.Forms.SaveFileDialog saveFileDialog;
private System.Windows.Forms.SplitContainer splitContainerBig;
private System.Windows.Forms.SplitContainer splitContainerSub;
private System.Windows.Forms.ListView xmlAttributeListView;
private System.Windows.Forms.TextBox xmlElementTextBox;
private System.Windows.Forms.ToolStripContainer toolStripContainer1;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton btnOpenFile;
private System.Windows.Forms.ToolStripMenuItem editToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem editElementToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem insertElementToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
private System.Windows.Forms.ToolStripMenuItem rToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem mnitmBeforeSelected;
private System.Windows.Forms.ToolStripMenuItem mnitmAfterSelected;
private System.Windows.Forms.ToolStripMenuItem mnitmAppend;
private System.Windows.Forms.ToolStripDropDownButton tbtnInsertElement;
private System.Windows.Forms.ToolStripMenuItem tmnitmBeforeSelected;
private System.Windows.Forms.ToolStripMenuItem tmnitmAfterSelected;
private System.Windows.Forms.ToolStripMenuItem tmnitmAppend;
private System.Windows.Forms.ToolStripButton tbtnDeleteElement;
private System.Windows.Forms.ToolStripSplitButton tsbtnSaveFile;
private System.Windows.Forms.ToolStripMenuItem saveAsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveAsToolStripMenuItem1;
private System.Windows.Forms.ToolStripButton tbtnEditElement;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
private System.Windows.Forms.ToolStripMenuItem tmnitmPrepend;
private System.Windows.Forms.ToolStripMenuItem mnitmPrepend;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem1;
}
}
InsertElementForm.designer.cs:
namespace XMLTreeView
{
partial class InsertElementForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose( bool disposing ) {
if ( disposing && ( components != null ) ) {
components.Dispose();
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent( ) {
this.txtContent = new System.Windows.Forms.TextBox();
this.pnTop = new System.Windows.Forms.Panel();
this.lblContent = new System.Windows.Forms.Label();
this.pnlBottom = new System.Windows.Forms.Panel();
this.btnCancel = new System.Windows.Forms.Button();
this.btnOk = new System.Windows.Forms.Button();
this.pnTop.SuspendLayout();
this.pnlBottom.SuspendLayout();
this.SuspendLayout();
//
// txtContent
//
this.txtContent.Location = new System.Drawing.Point( 12, 15 );
this.txtContent.Multiline = true;
this.txtContent.Name = "txtContent";
this.txtContent.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtContent.Size = new System.Drawing.Size( 677, 302 );
this.txtContent.TabIndex = 0;
//
// pnTop
//
this.pnTop.Controls.Add( this.lblContent );
this.pnTop.Controls.Add( this.txtContent );
this.pnTop.Dock = System.Windows.Forms.DockStyle.Top;
this.pnTop.Location = new System.Drawing.Point( 0, 0 );
this.pnTop.Name = "pnTop";
this.pnTop.Size = new System.Drawing.Size( 701, 394 );
this.pnTop.TabIndex = 1;
//
// lblContent
//
this.lblContent.AutoSize = true;
this.lblContent.Location = new System.Drawing.Point( 3, 0 );
this.lblContent.Name = "lblContent";
this.lblContent.Size = new System.Drawing.Size( 179, 12 );
this.lblContent.TabIndex = 1;
this.lblContent.Text = "Type Contents of New Element:";
//
// pnlBottom
//
this.pnlBottom.Controls.Add( this.btnCancel );
this.pnlBottom.Controls.Add( this.btnOk );
this.pnlBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
this.pnlBottom.Location = new System.Drawing.Point( 0, 323 );
this.pnlBottom.Name = "pnlBottom";
this.pnlBottom.Size = new System.Drawing.Size( 701, 30 );
this.pnlBottom.TabIndex = 2;
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point( 614, 3 );
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size( 75, 23 );
this.btnCancel.TabIndex = 1;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
//
// btnOk
//
this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOk.Location = new System.Drawing.Point( 533, 3 );
this.btnOk.Name = "btnOk";
this.btnOk.Size = new System.Drawing.Size( 75, 23 );
this.btnOk.TabIndex = 0;
this.btnOk.Text = "Insert";
this.btnOk.UseVisualStyleBackColor = true;
this.btnOk.Click += new System.EventHandler( this.btnOk_Click );
//
// InsertElementForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 12F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size( 701, 353 );
this.Controls.Add( this.pnlBottom );
this.Controls.Add( this.pnTop );
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "InsertElementForm";
this.Text = "Insert Element";
this.pnTop.ResumeLayout( false );
this.pnTop.PerformLayout();
this.pnlBottom.ResumeLayout( false );
this.ResumeLayout( false );
}
#endregion
private System.Windows.Forms.TextBox txtContent;
private System.Windows.Forms.Panel pnTop;
private System.Windows.Forms.Panel pnlBottom;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnOk;
private System.Windows.Forms.Label lblContent;
}
}
这些designer生成的代码要是手写的话肯定痛苦死了……呼。
===============================================================
All right,大功告成 ^ ^
如果还有什么药补充的话,应该就是:为了偷懒,我没有直接用DOM去操纵XML的编辑,而是通过.NET Framework的一个扩展——XPathNavigator来帮忙。这样效率虽说不太高,不过节省了很多手工的调用……诶总之算不上什么好办法就是了 =_=||