CSDN博客

img dhz123

XML的读写.....

发表于2004/10/25 14:29:00  858人阅读

分类: XML/XSL

////===================
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>

阅读全文
0 0

相关文章推荐

  • C++ XML读写工具-tinyxml
    yzshixiang 2017-11-09 09:11 3
  • XML文件读写
    tadpole099 2017-07-02 11:59 2
  • xml文件读写
    zuiaicc 2016-01-11 00:52 0
  • sqlite3 xml读写
    du_bingbing 2015-09-06 14:59 1
  • VB.net学习笔记(二十一)XML流的读写
    dzweather 2016-05-22 12:07 3510
  • C++ XML读写
    q1065822791 2013-01-25 15:07 0
  • C# 读写XML文件的方法详细总结
    u012391923 2016-11-04 17:38 2583
  • VC读写XML文件
    qq2399431200 2014-03-11 12:40 111
img
取 消
img