using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml ;namespace XMLOPER
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class XMLForm : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>    
private System.Windows.Forms.Button button1;
private System.Windows.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: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
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.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(this.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";
this.button2.Click += new System.EventHandler(this.create2);
// 
// 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.button3.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.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.Drawing.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.button4.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.Windows.Forms.Control[] {
  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

解决方案 »

  1.   


    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.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 = xmldoc.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" ) ;
    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 create2(object sender, System.EventArgs e)
    {
    XmlDocument doc = new XmlDocument ( ) ;
    doc.LoadXml ( 
    "<Employees> " +
    "<NO1>" +
    "<Name>Ma Jin Hu</Name>" +
    "<ZIP> 239000 </ZIP>" +
    "<Address>Feng Huang Street</Address>" +
    "<City>Chu Zhou City</City>" +
    "<State> ANHUI </State>" +
    "</NO1>" +
    "<NO2>" +
    "<Name>Wang Tian</Name>" +
    "<ZIP> 239000 </ZIP>" +
    "<Address>Lang Ya Street</Address>" +
    "<City>He Fei City</City>" +
    "<State> ANHUI </State>" +
    "</NO2>" +
    "<NO3>" +
    "<Name>Zou Wen Biao</Name>" +
    "<ZIP> 100000 </ZIP>" +
    "<Address>Sai Di Street</Address>" +
    "<City>Bei Jin City</City>" +
    "<State>Bei Jin</State>" +
    "</NO3>" +
    "</Employees>" ) ;     
    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 myItem = 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 , HorizontalAlignment.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 ( ) ) 
    {
    //判断当前读取得节点类型
    switch ( reader.NodeType )
    {
    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 ();

    }
    }    
    }