XML的读写.....

原创 2004年10月25日 14:29:00

////===================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml ;
using System.IO ;
using System.Xml.XPath ;
namespace xmlCreation
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.TreeView treeView1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.treeView1 = new System.Windows.Forms.TreeView();
   this.SuspendLayout();
   //
   // textBox1
   //
   this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    | System.Windows.Forms.AnchorStyles.Right)));
   this.textBox1.Location = new System.Drawing.Point(128, 8);
   this.textBox1.Multiline = true;
   this.textBox1.Name = "textBox1";
   this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
   this.textBox1.Size = new System.Drawing.Size(304, 312);
   this.textBox1.TabIndex = 0;
   this.textBox1.Text = "";
   //
   // button1
   //
   this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
   this.button1.Location = new System.Drawing.Point(48, 328);
   this.button1.Name = "button1";
   this.button1.TabIndex = 1;
   this.button1.Text = "create";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
   this.button2.Location = new System.Drawing.Point(320, 328);
   this.button2.Name = "button2";
   this.button2.TabIndex = 2;
   this.button2.Text = "path";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // treeView1
   //
   this.treeView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    | System.Windows.Forms.AnchorStyles.Left)
    | System.Windows.Forms.AnchorStyles.Right)));
   this.treeView1.ImageIndex = -1;
   this.treeView1.Location = new System.Drawing.Point(8, 8);
   this.treeView1.Name = "treeView1";
   this.treeView1.SelectedImageIndex = -1;
   this.treeView1.Size = new System.Drawing.Size(120, 312);
   this.treeView1.TabIndex = 3;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(440, 357);
   this.Controls.Add(this.treeView1);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.textBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
  private void LoadTree()
  {
     
   XPathDocument doc=new XPathDocument(@"../../custardOrders.xml");
           
   XPathNavigator nav=doc.CreateNavigator();
     
   XPathNodeIterator iterator=nav.Select("//customers");

     
   while(iterator.MoveNext()) 
   {
    XmlTreeNode node=new XmlTreeNode(iterator.Current);
    node.FillTreeView();
    XPathNodeIterator custIter=iterator.Current.SelectChildren("CustomerID","");
       
    while(custIter.MoveNext())
     node.Text=custIter.Current.Value;

    treeView1.Nodes.Add(node);
   }
     
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   //  this.LoadXml(@"../../Orders.xml");
  // this.LoadDocXml2 (@"../../Orders.xml");
   this.EditXmlDoc_Del(@"../../books.xml");
   this.LoadTree();
  }
  private void  EditXmlDoc_Del(string path)
  {
   XmlDocument dc=new XmlDocument();
   dc.Load(path);
   XmlNodeList id=dc.GetElementsByTagName("book");
   XmlNode  delNd=null;
   foreach(XmlNode nd in id)
   {
    if(nd.Attributes["ISBN"].Value =="3-123904-23-4")
    {
     this.textBox1.AppendText(nd.Name +":"+nd.InnerXml );
         delNd=nd; 
     
    }
   }
   if(delNd!=null)
   {
    delNd.ParentNode.RemoveChild(delNd);
    dc.Save(path);
   }
  }
  private void  EditXmlDoc_Add(string path)
  {
   XmlDocument doc=new XmlDocument();
   doc.Load (path);
   XmlElement newBook=doc.CreateElement ("book");
   newBook.SetAttribute("gener","network");
   newBook.SetAttribute("publicationdate","2002");
   newBook.SetAttribute("ISBN","3-123904-23-4");
   XmlElement newTitle=doc.CreateElement("title");
   newTitle.InnerText="C#网络程序设计";
   newBook.AppendChild(newTitle);
   XmlElement  newAuthor=doc.CreateElement("author");
   XmlElement  newAuthorFirstName=doc.CreateElement ("first-name");
   newAuthorFirstName.InnerText="Bill";
   XmlElement  newAuthorLastName=doc.CreateElement("last-name");
   newAuthorLastName.InnerText="Gate";
   newAuthor.AppendChild(newAuthorFirstName);
   newAuthor.AppendChild(newAuthorLastName);
   newBook.AppendChild(newAuthor);
   XmlElement  newPrice=doc.CreateElement ("price");
   newPrice.InnerText="100.00";
   newBook.AppendChild(newPrice);
   doc.DocumentElement .AppendChild(newBook);
   doc.Save(path);

  }
  private void  LoadDocXml2(string path)
  {
   System.Xml.XmlDocument xmldoc=new XmlDocument();
   xmldoc.Load(path);
   XmlNodeList nodes=xmldoc.SelectNodes("/Entity/Sqls/Sql");
   foreach(XmlNode nd in nodes)
   {
    this.textBox1.AppendText("/r/nSQL语句:/r/n");
    XmlAttributeCollection atrs=nd.Attributes ;
    if(atrs!=null)
    {
     foreach(XmlNode attr in atrs)
     {
      this.textBox1.AppendText(attr.Name +":"+attr.InnerText+"  ");
     }
    }
    this.textBox1.AppendText("/r/n参数列表/r/n");
    XmlNodeList sons=nd.ChildNodes ;
    foreach(XmlNode sn in sons)
    {
     foreach(XmlNode ac in sn.Attributes)
     {
      this.textBox1.AppendText(ac.Name +":"+ac.InnerText+"  " );
     }
     this.textBox1.AppendText("/r/n");
    }
    
   }
 
  }
  private void  LoadDocXml(string path)
  {
   System.Xml.XmlDocument xmldoc=new XmlDocument();
   xmldoc.Load(path);
      XmlNodeList nodes=xmldoc.SelectNodes("/Entity/Columns/Column");
 
   foreach(XmlNode node in nodes)
   {
    
    this.textBox1.AppendText(node.Name +"===/r/n");
       System.Xml.XmlAttributeCollection  ie=node.Attributes;
    if(ie==null)
     continue;
    foreach(XmlNode attr in ie)
    {
     this.textBox1.AppendText(attr.Name +":"+attr.InnerText+"  ");
    }
    this.textBox1.AppendText("/r/n");
   }
  }
  private void  LoadXml(string path)
  {
   System.Xml.XmlTextReader xr=new XmlTextReader(path);
   while(xr.Read ())
   {
    if(xr.NodeType==XmlNodeType.Element )
    {
     textBox1.AppendText("/r/nElement===="+xr.Name +":");
     if(xr.HasAttributes )
     {
      for(int i=0;i<xr.AttributeCount ;i++)
      {
       xr.MoveToAttribute (i);
       this.textBox1.AppendText(xr.Name +"=");
       this.textBox1.AppendText(xr.Value +"   ");
      }
     }
    }
   }
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   //xmlCreation.createSchema ct=new createSchema();
   //ct.Create(@"../../bookscode.xsd");
   this.EditXmlDoc_Add(@"../../books.xml");
   this.LoadDocXml2(@"../../orders.xml");
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   XPathDocument doc=new XPathDocument(@"../../books.xml");
      System.Xml.XPath.XPathNavigator nav=doc.CreateNavigator ();
   System.Xml.XPath.XPathNodeIterator itor=nav.SelectDescendants(XPathNodeType.Element ,true);
   while(itor.MoveNext ())
   {
    this.textBox1.AppendText(itor.Current.Name +":");
    System.Xml.XPath.XPathNodeIterator chd=itor.Current.SelectChildren(XPathNodeType.Text );
    while(chd.MoveNext ())
    {
     this.textBox1.AppendText(":"+chd.Current.Value );
    }
    this.textBox1.AppendText("/r/n");
   }
  }
 }
}
/////////orders.xml
<?xml version="1.0" encoding="utf-8" ?>
<Entity TabName="Orders">
 <Columns>
  <Column Name="OrderID" DataType="System.Int32" IsKey="true"></Column>
  <Column Name="CustomerID" DataType="System.Int32" IsKey="false"></Column>
  <Column Name="EmployeeID" DataType="System.Int32" IsKey="false"></Column>
  <Column Name="OrderDate" DataType="System.DateTime" IsKey="false"></Column>
  <Column Name="RequiredDate" DataType="System.DateTime" IsKey="false"></Column>
  <Column Name="ShippedDate" DataType="System.DateTime" IsKey="false"></Column>
  <Column Name="ShipVia" DataType="System.Int32" IsKey="false"></Column>
  <Column Name="Freight" DataType="System.Int32" IsKey="false"></Column>
  <Column Name="ShipName" DataType="System.String" IsKey="false"></Column>
  <Column Name="ShipAddress" DataType="System.String" IsKey="false"></Column>
  <Column Name="ShipCity" DataType="System.String" IsKey="false"></Column>
  <Column Name="ShipRegion" DataType="System.String" IsKey="false"></Column>
  <Column Name="ShipPostalCode" DataType="System.String" IsKey="false"></Column>
  <Column Name="ShipCountry" DataType="System.String" IsKey="false"></Column>
 </Columns>
 <Sqls>
  <Sql CommandName="InsertOrders"  CommandText="Insert into Orders(CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry) values(@CustomerID,@EmpID,@OrderDate,@ReqDate,@SpDate,@SpVia,@Freight,@SpName,@SpAddr,@SpCity,@SpRegion,@SpPostalCode,@SpCountry)    return   @@IDENTITY">
       <param name="@OrderID" type="System.Int32" size="4" sourceColoum="OrderID" Direction="Return"></param>
    <param name="@CustomerID" type="System.Int32" size="4" sourceColoum="CustomerID" Direction="Input"></param>
    <param name="@EmpID" type="System.Int32" size="4" sourceColoum="EmployeeID" Direction="Input"></param>
    <param name="@OrderDate" type="System.DateTime" size="8" sourceColoum="OrderDate" Direction="Input"></param>
    <param name="@ReqDate" type="System.DateTime" size="8" sourceColoum="RequiredDate" Direction="Input"></param>
    <param name="@SpDate" type="System.DateTime" size="8" sourceColoum="ShipDate" Direction="Input"></param>
    <param name="@SpVia" type="System.Int32" size="4" sourceColoum="ShipVia" Direction="Input"></param>
    <param name="@Freight" type="System.Double" size="8" sourceColoum="Freight" Direction="Input"></param>
    <param name="@SpName" type="System.String" size="8" sourceColoum="ShipName" Direction="Input"></param>
    <param name="@SpAddr" type="System.String" size="50" sourceColoum="ShipAddress" Direction="Input"></param>
    <param name="@SpCity" type="System.String" size="50" sourceColoum="ShipCity" Direction="Input"></param>
    <param name="@SpRegion" type="System.String" size="50" sourceColoum="ShipRegion" Direction="Input"></param>
    <param name="@SpPostalCode" type="System.String" size="50" sourceColoum="ShipPostalCode" Direction="Input"></param>
    <param name="@SpCountry" type="System.String" size="50" sourceColoum="ShipCountry" Direction="Input"></param>
  </Sql>
  <Sql CommandName="DeleteOrders"  CommandText="delete from  Orders where OrderID=@OrderID">
       <param name="@OrderID" type="System.Int32" size="4" sourceColoum="OrderID" Direction="Input"></param>
     </Sql>
     <Sql CommandName="UpdateOrders" CommandText="update Orders  set
           CustomerID=@CustomerID,
           EmployeeID=@EmpID,
           OrderDate=@OrderDate,
           RequiredDate=@ReqDate,
           ShippedDate=@SpDate,
           ShipVia=@SpVia,
           Freight=@Freight,
           ShipName=@SpName,
           ShipAddress=@SpAddr,
           ShipCity=@SpCity,
           ShipRegion=@SpRegion,
           ShipPostalCode=@SpPostalCode,
           ShipCountry=@SpCountry
           where     OrderID=@OrderID">
            <param name="@OrderID" type="System.Int32" size="4" sourceColoum="OrderID" Direction="Input"></param>
    <param name="@CustomerID" type="System.Int32" size="4" sourceColoum="CustomerID" Direction="Input"></param>
    <param name="@EmpID" type="System.Int32" size="4" sourceColoum="EmployeeID" Direction="Input"></param>
    <param name="@OrderDate" type="System.DateTime" size="8" sourceColoum="OrderDate" Direction="Input"></param>
    <param name="@ReqDate" type="System.DateTime" size="8" sourceColoum="RequiredDate" Direction="Input"></param>
    <param name="@SpDate" type="System.DateTime" size="8" sourceColoum="ShipDate" Direction="Input"></param>
    <param name="@SpVia" type="System.Int32" size="4" sourceColoum="ShipVia" Direction="Input"></param>
    <param name="@Freight" type="System.Double" size="8" sourceColoum="Freight" Direction="Input"></param>
    <param name="@SpName" type="System.String" size="8" sourceColoum="ShipName" Direction="Input"></param>
    <param name="@SpAddr" type="System.String" size="50" sourceColoum="ShipAddress" Direction="Input"></param>
    <param name="@SpCity" type="System.String" size="50" sourceColoum="ShipCity" Direction="Input"></param>
    <param name="@SpRegion" type="System.String" size="50" sourceColoum="ShipRegion" Direction="Input"></param>
    <param name="@SpPostalCode" type="System.String" size="50" sourceColoum="ShipPostalCode" Direction="Input"></param>
    <param name="@SpCountry" type="System.String" size="50" sourceColoum="ShipCountry" Direction="Input"></param>

     </Sql>
 
 </Sqls>
</Entity>
////books.xml
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
  <book genre="novel" publicationdate="1988" ISBN="0-141301-15-5">
    <title>C++程序设计</title>
    <author>
      <first-name>Roly</first-name>
      <last-name>Nacy</last-name>
    </author>
    <price>80.00</price>
  </book>
  <book genre="database" publicationdate="2000" ISBN="1-134559-34-4">
    <title>SQL数据库管理</title>
    <price>90.00</price>
    <author>
      <first-name>Elmer</first-name>
      <last-name>Fudd</last-name>
    </author>
  </book>
  <book gener="network" publicationdate="2002" ISBN="3-123904-23-4">
    <title>C#网络程序设计</title>
    <author>
      <first-name>Bill</first-name>
      <last-name>Gate</last-name>
    </author>
    <price>100.00</price>
  </book>
</bookstore>

关于XML的读写操作以及查询操作

我们在做一些项目的时候,往往会用XML来配置内部成员,部署他们之间的关系,使得程序运行起来不至于太混乱,今天就稍微讲解一下关于XML的一些用法。...
  • hasion
  • hasion
  • 2015年02月09日 10:42
  • 946

cocos2dx读写xml文件

首先包含头文件 #include "tinyxml2/tinyxml2.h" using namespace tinyxml2; //data.xml文件内容 Misty Vale 1...
  • TianTangYouZui
  • TianTangYouZui
  • 2016年03月16日 10:20
  • 1191

java的xml读写

一、采用Dom方式读写 1、Dom读 一次加载所有节点(xml文件过大可能出现问题),能保存xml的结构(更方便修改,整棵树都在内存中),是用DocumentBuilderFactor读入。 2、Do...
  • pc_gad
  • pc_gad
  • 2016年10月14日 00:38
  • 1588

xml文件读取和写入的实现方式小结

xml文件的读取和写入主要又两种,一种是XMLDocument,另一种是XMLtextwriter/xmltextreader。 1、XMLtextwriter/xmltextreader   ...
  • happy_rita
  • happy_rita
  • 2012年08月14日 14:35
  • 7484

VC读写XML文件

1、安装MSXML 4.0 SP2。在VC6中建立一个基于Dialog的工程。如图:在界面上放置3个编辑框、1个按钮控件。其中属性设置如下。编辑框: ID...
  • max2008
  • max2008
  • 2007年06月11日 11:16
  • 11429

Android学习笔记(八)之Android 读写xml文件

/** * XmlTool.java * 版权所有(C) 2012 * 创建:cuiran 2012-9-19 下午3:39:40 */ package com.wpn.xml; ...
  • cuiran
  • cuiran
  • 2012年09月21日 08:45
  • 7496

Unity3D C# 读写XML

效果 代码 using UnityEngine; using System.IO; using System.Xml; using System.Collections; public c...
  • AWNUXCVBN
  • AWNUXCVBN
  • 2013年07月03日 23:03
  • 9722

MFC中读取写入XML文件

第一步:新建一个对话框工程,在对话框上添加一个按钮《连接》和一个ComboBox下拉框,如图所示(注意:本次的按钮《断开》和按钮《保存图片》没有用到) 第二步:添加头文件 #import "msxm...
  • a443838772
  • a443838772
  • 2016年11月14日 10:23
  • 1884

C#编写XML读写类操作xml文件

下面的例子是用C# 在asp.net 中实现对xml的操作,环境是vs2005 , 自己写了一个操作类,然后在使用的时候调用它。实现:登录用户信息的添加、修改和删除,不使用数据库,只在本地存放一个xm...
  • fishpowersoft
  • fishpowersoft
  • 2007年12月21日 11:47
  • 44208

C/C++读写xml文件

MSXML2::IXMLDOMDocumentPtr pDoc; MSXML2::IXMLDOMElementPtr  xmlRoot ;  // 创建DOMDocument对象  HRE...
  • kyfvc
  • kyfvc
  • 2013年09月16日 17:51
  • 23345
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:XML的读写.....
举报原因:
原因补充:

(最多只允许输入30个字)