2022年WinForm的程序如何和XML结合 .pdf

上传人:C****o 文档编号:39746128 上传时间:2022-09-07 格式:PDF 页数:7 大小:49.50KB
返回 下载 相关 举报
2022年WinForm的程序如何和XML结合 .pdf_第1页
第1页 / 共7页
2022年WinForm的程序如何和XML结合 .pdf_第2页
第2页 / 共7页
点击查看更多>>
资源描述

《2022年WinForm的程序如何和XML结合 .pdf》由会员分享,可在线阅读,更多相关《2022年WinForm的程序如何和XML结合 .pdf(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Xml;namespace XMLOPER /Form1 的摘要说明。/public class XMLForm:System.Windows.Forms.Form /必需的设计器变量。/private System.Windows.Forms.Button button1;private System.Wi

2、ndows.Forms.Button button2;private System.Windows.Forms.Button button3;private System.Windows.Forms.ListView listView1;private System.Windows.Forms.Button button4;private System.ComponentModel.Container components=null;public XMLForm()/Windows 窗体设计器支持所必需的/InitializeComponent();/TODO:在InitializeCompo

3、nent 调用后添加任何构造函数代码/清理所有正在使用的资源。/protected override void Dispose(bool disposing)if(disposing)名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 7 页 -if(components!=null)components.Dispose();base.Dispose(disposing);#region Windows Form Designer generated code/设计器支持所需的方法-不要使用代码编辑器修改/此方法的内容。/private void InitializeComponen

4、t()this.button1=new System.Windows.Forms.Button();this.button2=new System.Windows.Forms.Button();this.button3=new System.Windows.Forms.Button();this.listView1=new System.Windows.Forms.ListView();this.button4=new System.Windows.Forms.Button();this.SuspendLayout();/button1/this.button1.Anchor=(System.

5、Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Right);this.button1.Location=new System.Drawing.Point(232,336);this.button1.Name=button1;this.button1.Size=new System.Drawing.Size(56,32);this.button1.TabIndex=0;this.button1.Text=创建 1;this.button1.Click+=new System.EventHandler(thi

6、s.create1);/button2/this.button2.Anchor=(System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Right);this.button2.Location=new System.Drawing.Point(288,336);this.button2.Name=button2;this.button2.Size=new System.Drawing.Size(56,32);this.button2.TabIndex=1;this.button2.Text=创建 2

7、;this.button2.Click+=new System.EventHandler(this.create2);名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 7 页 -/button3/this.button3.Anchor=(System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Right);this.button3.Location=new System.Drawing.Point(344,336);this.button3.Name=button3;this.button

8、3.Size=new System.Drawing.Size(56,32);this.button3.TabIndex=2;this.button3.Text=读取;this.button3.Click+=new System.EventHandler(this.button3_Click);/listView1/this.listView1.AllowColumnReorder=true;this.listView1.BackColor=System.Drawing.SystemColors.Info;this.listView1.CheckBoxes=true;this.listView1

9、.Dock=System.Windows.Forms.DockStyle.Top;this.listView1.ForeColor=System.Drawing.SystemColors.ActiveCaption;this.listView1.FullRowSelect=true;this.listView1.GridLines=true;this.listView1.HoverSelection=true;this.listView1.LabelEdit=true;this.listView1.Name=listView1;this.listView1.Size=new System.Dr

10、awing.Size(456,328);this.listView1.TabIndex=3;this.listView1.View=System.Windows.Forms.View.Details;/button4/this.button4.Anchor=(System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Right);this.button4.Location=new System.Drawing.Point(400,336);this.button4.Name=button4;this.b

11、utton4.Size=new System.Drawing.Size(56,32);this.button4.TabIndex=4;this.button4.Text=清空;this.button4.Click+=new System.EventHandler(this.button4_Click);/XMLForm/this.AutoScaleBaseSize=new System.Drawing.Size(6,14);this.ClientSize=new System.Drawing.Size(456,373);this.Controls.AddRange(new System.Win

12、dows.Forms.Control 名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 7 页 -this.button4,this.listView1,this.button3,this.button2,this.button1);this.Name=XMLForm;this.Text=XMLForm;this.Load+=new System.EventHandler(this.XMLForm_Load);this.ResumeLayout(false);#endregion/应用程序的主入口点。/STAThread static void Main()Application.

13、Run(new XMLForm();public void create1(object sender,System.EventArgs e)XmlDocument xmldoc;XmlNode xmlnode;XmlElement xmlelem;XmlElement xmlelem2;XmlText xmltext;xmldoc=new XmlDocument();/加入 XML 的声明段落xmlnode=xmldoc.CreateNode(XmlNodeType.XmlDeclaration,);xmldoc.AppendChild(xmlnode);/加入一个根元素xmlelem=xm

14、ldoc.CreateElement(,ROOT ,);xmltext=xmldoc.CreateTextNode(Root Text );xmlelem.AppendChild(xmltext);xmldoc.AppendChild(xmlelem);/加入另外一个元素xmlelem2=xmldoc.CreateElement(SampleElement );xmlelem2=xmldoc.CreateElement(,SampleElement ,);xmltext=xmldoc.CreateTextNode(The text of the sample element 名师资料总结-精品

15、资料欢迎下载-名师精心整理-第 4 页,共 7 页 -);xmlelem2.AppendChild(xmltext);xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2);/保存创建好的XML 文档try xmldoc.Save(c:data1.xml );MessageBox.Show(c:data1.xml-XML文件创建成功!);catch(Exception err)/显示错误信息MessageBox.Show(c:data1.xml-XML文件创建失败!);MessageBox.Show(err.Message);private void c

16、reate2(object sender,System.EventArgs e)XmlDocument doc=new XmlDocument();doc.LoadXml(+Ma Jin Hu +239000 +Feng Huang Street +Chu Zhou City +ANHUI +Wang Tian +239000 +Lang Ya Street +He Fei City +ANHUI +Zou Wen Biao +100000 +Sai Di Street +Bei Jin City +Bei Jin +);名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 7 页 -

17、try doc.Save(c:data2.xml );MessageBox.Show(c:data2.xml-XML文件创建成功!);catch(Exception err)/显示错误信息MessageBox.Show(c:data2.xml-XML文件创建失败!);MessageBox.Show(err.Message);private void XMLForm_Load(object sender,System.EventArgs e)private void button3_Click(object sender,System.EventArgs e)ListViewItem myIte

18、m=new ListViewItem();/构建 listView 组件listView1.Columns.Clear();listView1.Items.Clear();listView1.Columns.Add(Name ,80,HorizontalAlignment.Left);listView1.Columns.Add(Zip ,80,HorizontalAlignment.Left);listView1.Columns.Add(Address ,80,HorizontalAlignment.Right);listView1.Columns.Add(City ,80,Horizonta

19、lAlignment.Right);listView1.Columns.Add(State ,80,HorizontalAlignment.Center);XmlNodeReader reader=null;try string s=;XmlDocument doc=new XmlDocument();/装入指定的XML 文档doc.Load(C:data2.xml );/设定 XmlNodeReader 对象来打开XML 文件reader=new XmlNodeReader(doc);/读取 XML 文件中的数据,并显示出来while(reader.Read()/判断当前读取得节点类型swi

20、tch(reader.NodeType)名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 7 页 -case XmlNodeType.Element:s=reader.Name;break;case XmlNodeType.Text:if(s.Equals(Name )myItem=listView1.Items.Add(reader.Value);else myItem.SubItems.Add(reader.Value);break;finally /清除打开的数据流if(reader!=null)reader.Close();private void button4_Click(object sender,System.EventArgs e)/listView1.Items.Clear();/listView1.Columns.Clear();listView1.Clear();名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 7 页 -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁