- 浏览: 3050678 次
- 性别:
- 来自: 海外
文章分类
- 全部博客 (430)
- Programming Languages (23)
- Compiler (20)
- Virtual Machine (57)
- Garbage Collection (4)
- HotSpot VM (26)
- Mono (2)
- SSCLI Rotor (1)
- Harmony (0)
- DLR (19)
- Ruby (28)
- C# (38)
- F# (3)
- Haskell (0)
- Scheme (1)
- Regular Expression (5)
- Python (4)
- ECMAScript (2)
- JavaScript (18)
- ActionScript (7)
- Squirrel (2)
- C (6)
- C++ (10)
- D (2)
- .NET (13)
- Java (86)
- Scala (1)
- Groovy (3)
- Optimization (6)
- Data Structure and Algorithm (3)
- Books (4)
- WPF (1)
- Game Engines (7)
- 吉里吉里 (12)
- UML (1)
- Reverse Engineering (11)
- NSIS (4)
- Utilities (3)
- Design Patterns (1)
- Visual Studio (9)
- Windows 7 (3)
- x86 Assembler (1)
- Android (2)
- School Assignment / Test (6)
- Anti-virus (1)
- REST (1)
- Profiling (1)
- misc (39)
- NetOA (12)
- rant (6)
- anime (5)
- Links (12)
- CLR (7)
- GC (1)
- OpenJDK (2)
- JVM (4)
- KVM (0)
- Rhino (1)
- LINQ (2)
- JScript (0)
- Nashorn (0)
- Dalvik (1)
- DTrace (0)
- LLVM (0)
- MSIL (0)
最新评论
-
mldxs:
虽然很多还是看不懂,写的很好!
虚拟机随谈(一):解释器,树遍历解释器,基于栈与基于寄存器,大杂烩 -
HanyuKing:
Java的多维数组 -
funnyone:
Java 8的default method与method resolution -
ljs_nogard:
Xamarin workbook - .Net Core 中不 ...
LINQ的恶搞…… -
txm119161336:
allocatestlye1 顺序为 // Fields o ...
最近做的两次Java/JVM分享的概要
题目:
开发环境:
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里拖出界面,如下:
原图链接
然后把相应的实现代码写出来:
XMLTreeViewForm.cs:
为了实现编辑功能,还另外加了一个非常简单的Form来做文本插入,
InsertElementForm.cs:
到此,实现的功能有(以下内容来自交作业时的readme):
rev_screenshot.jpg:
原图链接
screenshot.jpg:
原图链接
上面两个Form的designer生成代码分别如下:
XMLTreeView.designer.cs:
InsertElementForm.designer.cs:
这些designer生成的代码要是手写的话肯定痛苦死了……呼。
===============================================================
All right,大功告成 ^ ^
如果还有什么药补充的话,应该就是:为了偷懒,我没有直接用DOM去操纵XML的编辑,而是通过.NET Framework的一个扩展——XPathNavigator来帮忙。这样效率虽说不太高,不过节省了很多手工的调用……诶总之算不上什么好办法就是了 =_=||
引用
第三章实践作业
1.编写一个窗口应用程序,在窗口中有一个Tree控件和一个“浏览”按钮。单击“浏览”按钮,可以打开一个“打开文件”对话框,选择一个XML文件,在Tree中显示XML所对应的树形结构。
2.扩展创新功能(可选的附加功能,属加分项目):对树结构的处理上,例如添加、删除节点。能保存到原XML文件中。
作业要求:
窗口应用程序
功能:
单击按钮——弹出文件选择框——选取XML文件——显示XML文件的树形结构
会用到的控件:Tree控件。
作业涉及的代码内容:
1.编写一个窗口应用程序,在窗口中有一个Tree控件和一个“浏览”按钮。单击“浏览”按钮,可以打开一个“打开文件”对话框,选择一个XML文件,在Tree中显示XML所对应的树形结构。
2.扩展创新功能(可选的附加功能,属加分项目):对树结构的处理上,例如添加、删除节点。能保存到原XML文件中。
作业要求:
窗口应用程序
功能:
单击按钮——弹出文件选择框——选取XML文件——显示XML文件的树形结构
会用到的控件:Tree控件。
作业涉及的代码内容:
- Button以及它的事件响应,调用文件选择对话框。
- OpenFileDialog类,以及其中部分方法,属性。(主要使用该类中获取文件地址相关的一些属性和方法)
- XmlNode和XmlDocument类(用DOM浏览XML)
- TreeView类:将DOM中的节点传递到TreeView节点中,实现方法因人而异,像数据结构中的访问树节点,也可以用递归实现。一种递归函数实现方法是:将XmlNode和TreeNodeCollection对象作为参数
开发环境:
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里拖出界面,如下:
原图链接
然后把相应的实现代码写出来:
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):
引用
.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上编译运行。
==============================================
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上编译运行。
rev_screenshot.jpg:
原图链接
screenshot.jpg:
原图链接
上面两个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来帮忙。这样效率虽说不太高,不过节省了很多手工的调用……诶总之算不上什么好办法就是了 =_=||
评论
2 楼
RednaxelaFX
2007-11-26
我觉得这次应该已经比系列的上一篇好一些了?
因为只要是跟GUI相关的东西,代码都未必"好看"到哪里去.
Designer生成的都是...怎么说,垃圾代码? 或者温和点说,是有存在的必要但却不想手写的代码.所以这次把designer生成的代码都放在最后了...
然后也把其中最有用的代码放在前面说了...
-
或许应该把编译出来的exe也一起放出来? 直接使用的话心情会轻松很多 XD
写了这作业之后,自己也偶尔会用这程序...不过更多的时候还是直接开UEStudio来编辑了
因为只要是跟GUI相关的东西,代码都未必"好看"到哪里去.
Designer生成的都是...怎么说,垃圾代码? 或者温和点说,是有存在的必要但却不想手写的代码.所以这次把designer生成的代码都放在最后了...
然后也把其中最有用的代码放在前面说了...
-
或许应该把编译出来的exe也一起放出来? 直接使用的话心情会轻松很多 XD
写了这作业之后,自己也偶尔会用这程序...不过更多的时候还是直接开UEStudio来编辑了
1 楼
lwwin
2007-11-26
感觉一堆很复杂的CODE- -眼睛麻麻的~
还是建议选择一些有趣的绍介〇〇
还是建议选择一些有趣的绍介〇〇
发表评论
-
字符串的一般封装方式的内存布局 (1): 元数据与字符串内容,整体还是分离?
2013-11-07 17:44 22402(Disclaimer:未经许可请 ... -
字符串的一般封装方式的内存布局
2013-11-01 12:55 0(Disclaimer:未经许可请 ... -
关于string,内存布局,C++ std::string,CoW
2013-10-30 20:45 0(Disclaimer:未经许可请 ... -
对象的重量
2011-08-21 17:15 0http://domino.research.ibm.com/ ... -
GetCustomAttribute()每次都返回新Attribute实例
2009-11-10 10:30 0Jeffrey Zhao: 一次失败的尝试(上):原来GetC ... -
委托与方法和隐藏参数
2009-09-07 15:32 3309之前正好发了些帖子是关于CLR里的委托的,然后看到老赵说事件也 ... -
要让CLR挂掉的话(第二弹)……
2009-09-04 03:26 12875(Disclaimer:如果需要转 ... -
要让CLR挂掉的话……
2009-09-02 16:53 4780(Disclaimer:如果需要转载请先与我联系。 作者:Re ... -
趣味编程:函数式链表的快速排序
2009-08-31 08:53 3447(恢复自2009-08-28的备份 ... -
事件处理器导致内存泄漏
2009-08-25 15:03 0Memory leak via event handlers ... -
C# 3.0的类型推导
2009-08-23 12:24 0Howard Dierking: Lambda, Lambda ... -
把lock的意思给弄混了 T T
2009-08-20 17:49 2601悲剧啊……前几天有个同学不停问我Java里的同步问题,今天写C ... -
把IEnumerable<T>和IObservable<T>粘起来?
2009-07-23 03:02 0Channel 9: Expert to Expert: Br ... -
Scott Peterson: Variance, Thy Name is Ambiguity
2009-07-01 23:49 1638原文作者:Scott Peterson 原文地址:http:/ ... -
void无法协变
2009-06-30 11:17 0Eric Lippert The void is invari ... -
同一个表达式算出来的浮点数结果会不相等?
2009-05-30 03:27 0浮点数有很多可把玩的地方。例如下面这段C程序: #includ ... -
C#开始默认引用Microsoft.CSharp.dll
2009-05-20 16:14 0记得VB6的运行时么?留意到VB.NET的程序都需要额外的VB ... -
反射与显式实现接口的方法
2009-05-20 11:43 4060在前一帖里,我用到了下面三处Expression.Call() ... -
看到一个关于ref参数与多态的问题,记一下
2009-05-18 10:48 1943刚才读到Alan McGovern的一帖,问为什么形式参数是r ... -
C#的+=运算符两例
2009-05-06 18:18 2036刚偶尔看到了justjavac写的java解惑 - 半斤八两( ...
相关推荐
【标题】"下载煎蛋无聊图--简单Web浏览"是一个关于如何实现一个简单的网页浏览功能,特别是针对“煎蛋网”无聊图下载的项目。这个项目可能是为了帮助用户方便地获取和保存煎蛋网上有趣的图片资源。从标签“源码”和...
C#是.NET框架下的一个面向对象的编程语言,具有丰富的库和强大的功能,常用于开发桌面应用程序,如我们这里的Markdown编辑器。 这个“MarkDown编辑器”项目是使用C#编写的,表明它利用了.NET Framework 3.5的特性,...
标题中的“一个能发邮件的文本编辑器”指的是一个使用Python编程语言开发的应用程序,它具备基本的文本编辑功能,并且能够发送电子邮件。这个编辑器可能是为了方便用户在编辑文本后直接通过程序发送邮件而设计的,...
"无聊的C语言作业"这个标题可能是在幽默地表达对作业的调侃,但实际上,C语言的学习和实践对于提升编程技能至关重要。这份作业可能是包含了一系列C语言的编程练习,旨在帮助学习者巩固基础知识,掌握核心概念。 ...
标题中的“无聊时做的一个简单的bbs”表明这是一个个人项目,可能是一个基于Web的论坛系统,主要用于学习和实践编程技能。描述中提到的功能包括留言、评论以及上传资源,这暗示了该BBS至少包含了基本的用户互动和...
PsPad是一款广受好评的文本编辑器,尤其适合程序员和日常文本处理工作。其免费且开源的特性使得它在用户群中具有很高的受欢迎度。这款编辑器不仅功能强大,而且易于使用,能够满足各种文本编辑需求。 一、主要功能 ...
一个简单的 Javascript/jQuery 框架,允许快速创建、部署和扩展增量游戏 目标 这样做的目的是为其他增量游戏爱好者创建一个非常易于使用的 JavaScript 框架!我经常在网上看到很多人想开始自己的事业,但缺乏时间或...
本来无聊想写个动画编辑器,Java已经用的熟悉了,所以写一半停了,改用c#,边学习边做,这个是个半成品,只是提出来给需要学习Java做gui开发的朋友使用,大牛高手勿下!代码很简单,没有注释,一看就能懂!
无聊时用java编写的java编辑器,主要是嫌Eclipse占用资源太大,于是用java实现了一个小型的编辑器。 说明:1.编写java类时会自动生成java文件,不需要重命名 2.编写其它文件时要重命名,双击树目录或标签即可命名 3....
标题“无聊的盒子_单片机_舵机_无聊的盒子”揭示了这是一个与电子制作相关的项目,其中涉及单片机和舵机技术。在这个项目中,“无聊的盒子”可能是一个创新的装置,通过舵机来实现其开关功能,增加了互动性和趣味性...
Java Web练手做的一个无聊网页小游戏.zipJava Web练手做的一个无聊网页小游戏.zip Java Web练手做的一个无聊网页小游戏.zipJava Web练手做的一个无聊网页小游戏.zip Java Web练手做的一个无聊网页小游戏.zipJava Web...
在已经有xml的情况下,快速生成javabean代码,不需要自己再手打大量无聊的代码。
标题 "一个非常非常无聊的东西" 可能是在戏谑地表达这个软件或工具的独特性,它可能是一个非主流或者趣味性的应用,旨在让用户的键盘体验变得与众不同。在IT领域,有时候创新和趣味性的结合可以带来出人意料的用户...
这是我自己无聊时用wpf写的一个编辑host的工具,可以将配置保存起来一键替换,使用时需要先安装.netframework3.5
Java Web练手做的一个无聊网页小游戏,持续完善中 Java Web练手做的一个无聊网页小游戏,持续完善中 Java Web练手做的一个无聊网页小游戏,持续完善中 Java Web练手做的一个无聊网页小游戏,持续完善中 Java Web练手...
很多恶意程序会往系统注册表中添加大量的映像劫持,导致一大批安全软件无法使用,此时一个一个删除这些映像劫持是件费时费劲而且很无聊的事情。映像劫持编辑器提供了基本的对映像劫持进行编辑的功能,可以批量删除...
“无聊与无聊”游戏的描述中提到它可以单机玩,这意味着它可能是一个基于本地的、无网络需求的小程序。开发者可能使用了VB.NET的事件驱动编程模型,监听用户的键盘或鼠标事件,以此来响应玩家的操作,比如开始游戏、...