www.pudn.com > XMLOpertion.rar > Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml ;
using System.Xml .XPath ;
using System.Xml .Xsl ;
using System.IO ;
namespace youxi
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtDocument;
private System.Windows.Forms.Button btnPlus;
private System.Windows.Forms.Button btnMinus;
private System.Windows.Forms.Button Button1;
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtBookTitle;
private System.Windows.Forms.TextBox txtNewPrice;
private System.Windows.Forms.Button seekbutton;
private System.Windows.Forms.Label cxLabel;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button btnChangeSingle;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Button xsltButton;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox deleTextBox;
private System.Windows.Forms.Button deleButton;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private XmlDocument doc;
private string fileName ;
private System.Windows.Forms.OpenFileDialog ofd;
private System.Windows.Forms.SaveFileDialog sfd;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnLoad = new System.Windows.Forms.Button();
this.Button1 = new System.Windows.Forms.Button();
this.btnMinus = new System.Windows.Forms.Button();
this.btnPlus = new System.Windows.Forms.Button();
this.txtDocument = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button5 = new System.Windows.Forms.Button();
this.btnChangeSingle = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.cxLabel = new System.Windows.Forms.Label();
this.seekbutton = new System.Windows.Forms.Button();
this.txtNewPrice = new System.Windows.Forms.TextBox();
this.txtBookTitle = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.addButton = new System.Windows.Forms.Button();
this.textBox5 = new System.Windows.Forms.TextBox();
this.xsltButton = new System.Windows.Forms.Button();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.button8 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.deleButton = new System.Windows.Forms.Button();
this.deleTextBox = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.ofd = new System.Windows.Forms.OpenFileDialog();
this.sfd = new System.Windows.Forms.SaveFileDialog();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.groupBox4.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnLoad);
this.groupBox1.Controls.Add(this.Button1);
this.groupBox1.Controls.Add(this.btnMinus);
this.groupBox1.Controls.Add(this.btnPlus);
this.groupBox1.Controls.Add(this.txtDocument);
this.groupBox1.Location = new System.Drawing.Point(8, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(328, 240);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// btnLoad
//
this.btnLoad.Location = new System.Drawing.Point(192, 208);
this.btnLoad.Name = "btnLoad";
this.btnLoad.TabIndex = 4;
this.btnLoad.Text = "加载文档";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// Button1
//
this.Button1.Location = new System.Drawing.Point(88, 208);
this.Button1.Name = "Button1";
this.Button1.TabIndex = 3;
this.Button1.Text = "选择文档";
this.Button1.Click += new System.EventHandler(this.Button1_Click);
//
// btnMinus
//
this.btnMinus.Location = new System.Drawing.Point(48, 208);
this.btnMinus.Name = "btnMinus";
this.btnMinus.Size = new System.Drawing.Size(24, 23);
this.btnMinus.TabIndex = 2;
this.btnMinus.Text = "-";
this.btnMinus.Click += new System.EventHandler(this.btnMinus_Click);
//
// btnPlus
//
this.btnPlus.Location = new System.Drawing.Point(16, 208);
this.btnPlus.Name = "btnPlus";
this.btnPlus.Size = new System.Drawing.Size(24, 23);
this.btnPlus.TabIndex = 1;
this.btnPlus.Text = "+";
this.btnPlus.Click += new System.EventHandler(this.btnPlus_Click);
//
// txtDocument
//
this.txtDocument.Location = new System.Drawing.Point(8, 16);
this.txtDocument.Multiline = true;
this.txtDocument.Name = "txtDocument";
this.txtDocument.Size = new System.Drawing.Size(312, 184);
this.txtDocument.TabIndex = 0;
this.txtDocument.Text = "textBox1";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.button5);
this.groupBox2.Controls.Add(this.btnChangeSingle);
this.groupBox2.Controls.Add(this.button3);
this.groupBox2.Controls.Add(this.button2);
this.groupBox2.Controls.Add(this.cxLabel);
this.groupBox2.Controls.Add(this.seekbutton);
this.groupBox2.Controls.Add(this.txtNewPrice);
this.groupBox2.Controls.Add(this.txtBookTitle);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Location = new System.Drawing.Point(344, 0);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(272, 240);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "groupBox2";
//
// button5
//
this.button5.Location = new System.Drawing.Point(8, 184);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(128, 23);
this.button5.TabIndex = 9;
this.button5.Text = "改属性priceType";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// btnChangeSingle
//
this.btnChangeSingle.Location = new System.Drawing.Point(8, 128);
this.btnChangeSingle.Name = "btnChangeSingle";
this.btnChangeSingle.Size = new System.Drawing.Size(128, 23);
this.btnChangeSingle.TabIndex = 8;
this.btnChangeSingle.Text = "btnChangeSingle";
this.btnChangeSingle.Click += new System.EventHandler(this.btnChangeSingle_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(136, 128);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(128, 23);
this.button3.TabIndex = 7;
this.button3.Text = "改价格price";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 184);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(128, 23);
this.button2.TabIndex = 6;
this.button2.Text = "改名称title";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// cxLabel
//
this.cxLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.cxLabel.Location = new System.Drawing.Point(40, 88);
this.cxLabel.Name = "cxLabel";
this.cxLabel.Size = new System.Drawing.Size(184, 23);
this.cxLabel.TabIndex = 5;
this.cxLabel.Text = "查询结果";
//
// seekbutton
//
this.seekbutton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.seekbutton.Location = new System.Drawing.Point(232, 24);
this.seekbutton.Name = "seekbutton";
this.seekbutton.Size = new System.Drawing.Size(32, 56);
this.seekbutton.TabIndex = 4;
this.seekbutton.Text = "查询";
this.seekbutton.Click += new System.EventHandler(this.seekbutton_Click);
//
// txtNewPrice
//
this.txtNewPrice.Location = new System.Drawing.Point(72, 56);
this.txtNewPrice.Name = "txtNewPrice";
this.txtNewPrice.Size = new System.Drawing.Size(136, 21);
this.txtNewPrice.TabIndex = 3;
this.txtNewPrice.Text = "textBox3";
this.txtNewPrice.MouseDown += new System.Windows.Forms.MouseEventHandler(this.txtNewPrice_MouseDown);
//
// txtBookTitle
//
this.txtBookTitle.Location = new System.Drawing.Point(72, 24);
this.txtBookTitle.Name = "txtBookTitle";
this.txtBookTitle.Size = new System.Drawing.Size(136, 21);
this.txtBookTitle.TabIndex = 2;
this.txtBookTitle.Text = "textBox2";
this.txtBookTitle.MouseDown += new System.Windows.Forms.MouseEventHandler(this.txtBookTitle_MouseDown);
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 23);
this.label2.TabIndex = 1;
this.label2.Text = "改为";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 0;
this.label1.Text = "名称";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// groupBox3
//
this.groupBox3.Controls.Add(this.textBox4);
this.groupBox3.Controls.Add(this.label6);
this.groupBox3.Controls.Add(this.label5);
this.groupBox3.Controls.Add(this.textBox2);
this.groupBox3.Controls.Add(this.textBox3);
this.groupBox3.Controls.Add(this.textBox1);
this.groupBox3.Controls.Add(this.label4);
this.groupBox3.Controls.Add(this.label3);
this.groupBox3.Controls.Add(this.addButton);
this.groupBox3.Location = new System.Drawing.Point(8, 240);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(504, 104);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "groupBox3";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(280, 64);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(128, 21);
this.textBox4.TabIndex = 7;
this.textBox4.Text = "";
//
// label6
//
this.label6.Location = new System.Drawing.Point(208, 56);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(64, 23);
this.label6.TabIndex = 6;
this.label6.Text = "priceType";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label5
//
this.label5.Location = new System.Drawing.Point(16, 64);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(48, 23);
this.label5.TabIndex = 5;
this.label5.Text = "label5";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(280, 24);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(128, 21);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(80, 64);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(120, 21);
this.textBox3.TabIndex = 3;
this.textBox3.Text = "";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(208, 24);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(48, 23);
this.label4.TabIndex = 1;
this.label4.Text = "price";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 24);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(48, 23);
this.label3.TabIndex = 0;
this.label3.Text = "title";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// addButton
//
this.addButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.addButton.Location = new System.Drawing.Point(424, 24);
this.addButton.Name = "addButton";
this.addButton.Size = new System.Drawing.Size(75, 64);
this.addButton.TabIndex = 8;
this.addButton.Text = "添加数据";
this.addButton.Click += new System.EventHandler(this.addButton_Click);
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(520, 248);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(96, 21);
this.textBox5.TabIndex = 3;
this.textBox5.Text = "转换后的文件名";
this.textBox5.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox5_MouseDown);
//
// xsltButton
//
this.xsltButton.Location = new System.Drawing.Point(520, 280);
this.xsltButton.Name = "xsltButton";
this.xsltButton.Size = new System.Drawing.Size(96, 56);
this.xsltButton.TabIndex = 4;
this.xsltButton.Text = "XML+XSL->HTML 转换成";
this.xsltButton.Click += new System.EventHandler(this.xsltButton_Click);
//
// groupBox4
//
this.groupBox4.Controls.Add(this.button8);
this.groupBox4.Controls.Add(this.button7);
this.groupBox4.Controls.Add(this.deleButton);
this.groupBox4.Controls.Add(this.deleTextBox);
this.groupBox4.Controls.Add(this.label7);
this.groupBox4.Location = new System.Drawing.Point(8, 352);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(504, 100);
this.groupBox4.TabIndex = 5;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "groupBox4";
//
// button8
//
this.button8.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button8.Location = new System.Drawing.Point(408, 32);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(75, 56);
this.button8.TabIndex = 4;
this.button8.Text = "删除一个属性priceType";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button7
//
this.button7.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button7.Location = new System.Drawing.Point(312, 32);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(75, 56);
this.button7.TabIndex = 3;
this.button7.Text = "删除一组数据中的最后一个结点";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// deleButton
//
this.deleButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.deleButton.Location = new System.Drawing.Point(216, 32);
this.deleButton.Name = "deleButton";
this.deleButton.Size = new System.Drawing.Size(75, 56);
this.deleButton.TabIndex = 2;
this.deleButton.Text = "删除一组数据";
this.deleButton.Click += new System.EventHandler(this.deleButton_Click);
//
// deleTextBox
//
this.deleTextBox.Location = new System.Drawing.Point(8, 56);
this.deleTextBox.Name = "deleTextBox";
this.deleTextBox.Size = new System.Drawing.Size(192, 21);
this.deleTextBox.TabIndex = 1;
this.deleTextBox.Text = "BookTitle";
//
// label7
//
this.label7.Location = new System.Drawing.Point(8, 17);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(192, 23);
this.label7.TabIndex = 0;
this.label7.Text = "输入欲删除结点名称(title):";
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button4
//
this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button4.Location = new System.Drawing.Point(520, 360);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(96, 40);
this.button4.TabIndex = 6;
this.button4.Text = "ie 显示";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// btnClose
//
this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnClose.Location = new System.Drawing.Point(520, 408);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(96, 40);
this.btnClose.TabIndex = 7;
this.btnClose.Text = "Close";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(624, 461);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.button4);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.xsltButton);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.textBox5);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.groupBox4.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void btnPlus_Click(object sender, System.EventArgs e)
{
if( txtDocument.Font.SizeInPoints < 32 )
{
txtDocument.Font = new Font("Microsoft Sans Serif", txtDocument.Font.Size + 2);
}
}
private void btnMinus_Click(object sender, System.EventArgs e)
{
if( txtDocument.Font.SizeInPoints > 8 )
{
txtDocument.Font = new Font("Microsoft Sans Serif", txtDocument.Font.Size - 2);
}
}
private void btnLoad_Click(object sender, System.EventArgs e)
{
StreamReader reader = new StreamReader(fileName);
txtDocument.Text = reader.ReadToEnd();
reader.Close();
doc =new XmlDocument();
doc.PreserveWhitespace = false;
doc.LoadXml(txtDocument.Text);
// '激活其他功能
// nodeEdit.Enabled = true;
}
private void btnChangeSingle_Click(object sender, System.EventArgs e)
{
XmlNode book ,priceNode;
XmlNode root = doc.DocumentElement;
try
{
// 'Point to the book to modify
book = root.SelectSingleNode("descendant::book[title='" + txtBookTitle.Text + "']");
// 'Point to the price node 修改title名称下面的价格内容,因为价格paice为最后一个节点
priceNode = book.ChildNodes[1]; //'lastchild;
// 'Change the price on the book.
priceNode.InnerText = txtNewPrice.Text;
// 'display new prices
doc.Save(fileName);
StreamReader reade = new StreamReader(fileName);
txtDocument.Text = reade .ReadToEnd();
reade .Close();
}
catch(Exception e1)
{
MessageBox.Show(e1.ToString(), "你输入的book不存在!",MessageBoxButtons.OK);
}
}
public void IncreasePrice( XmlNode node )
{
if (node.Name == "price")
{
node = node.FirstChild;
decimal price = decimal.Parse(node.Value);
// ' Increase all the book prices by 5%
string Newprice = ((decimal)(price * (new decimal(1.05)))).ToString("#.00");
Console.WriteLine("Old Price = " + node.Value + "\tNew price = " + Newprice);
node.Value = Newprice;
node = node.FirstChild;
while(node!=null)
{
IncreasePrice(node);
node = node.NextSibling;
}
}
}
private void btnClose_Click(object sender, System.EventArgs e)
{
this.Close ();
}
private void Button1_Click(object sender, System.EventArgs e)
{
ofd = new OpenFileDialog();
string str;
sfd =new SaveFileDialog();
ofd.InitialDirectory = "../4.15.xml";
ofd.FileName = "4.15.xml";
ofd.Filter = "XML文件(*.xml)|*.xml";
if(ofd.ShowDialog()== DialogResult.OK )
{
str = ofd.FileName;
fileName = str;
sfd.FileName = ofd.FileName;
}
else
{
MessageBox.Show ("error啦");
}
btnLoad.Enabled = true;
}
private void button2_Click(object sender, System.EventArgs e)
{
XmlNode book ,priceNode ;
XmlNode root = doc.DocumentElement;
try
{
//'Point to the book to modify
book = root.SelectSingleNode("descendant::book[title='" + txtBookTitle.Text + "']");
// 'Point to the price node 修改title名称的内容
priceNode = book.FirstChild;
// '.LastChild()
// 'Change the price on the book.
priceNode.InnerText = txtNewPrice.Text;
// 'display new prices
doc.Save(fileName);
StreamReader reader = new StreamReader(fileName);
txtDocument.Text = reader.ReadToEnd();
reader.Close();
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString(), "No such book exists", MessageBoxButtons.OK);
}
}
private void button3_Click(object sender, System.EventArgs e)
{
XmlNode book, priceNode;
XmlNode root = doc.DocumentElement;
try
{
// 'Point to the book to modify
book = root.SelectSingleNode("descendant::book[title='" + txtBookTitle.Text + "']");
// '修改满足上面条件的book元素的第(2)个zi节点的内容
priceNode = book.LastChild;
// 'Change the price on the book.
priceNode.InnerText = txtNewPrice.Text;
// 'display new prices
doc.Save(fileName);
StreamReader reader =new StreamReader(fileName);
txtDocument.Text = reader.ReadToEnd();
reader.Close();
}
catch(Exception e1)
{
MessageBox.Show(e1.ToString(), "你输入的book不存在!", MessageBoxButtons.OK);
}
}
private void button5_Click(object sender, System.EventArgs e)
{
XmlNode book ,priceNode ;
XmlNode root = doc.DocumentElement;
XmlElement bookc;
try
{
// 'Point to the book to modify
book = root.SelectSingleNode("descendant::book[title='" + txtBookTitle.Text + "']");
bookc =(XmlElement)(book.ChildNodes[2]);
bookc.SetAttribute("priceType", txtNewPrice.Text);
// 'display new prices
doc.Save(fileName);
StreamReader reader = new StreamReader(fileName);
txtDocument.Text = reader.ReadToEnd();
reader.Close();
}
catch
{
MessageBox.Show("友情提示:", "你输入的删除条件不存在!",MessageBoxButtons.OK);
}
}
private void deleButton_Click(object sender, System.EventArgs e)
{
XmlNode book ;
XmlNode root = doc.DocumentElement;
try
{
// '定位到满足删除条件的结点:book上
book = root.SelectSingleNode("descendant::book[title='" + deleTextBox.Text + "']");
book.RemoveAll();// '删除book的所有子结点
// 'display new prices
doc.Save(fileName);
StreamReader reader = new StreamReader(fileName);
txtDocument.Text = reader.ReadToEnd();
reader.Close();
}
catch
{
MessageBox.Show("友情提示:", "你输入的删除条件不存在!", MessageBoxButtons.OK);
}
}
private void addButton_Click(object sender, System.EventArgs e)
{
XmlNode booka ;
XmlNode priceNode;
XmlNode bookb;
XmlNode root = doc.DocumentElement;
XmlNode myElement1 , myElement2 , myElement3 , myElement4 ;
// ' Dim myAttribute As XmlAttribute
XmlElement bookc ;
try
{//'相当于添加一条数据记录!有3个字段:title,price,name
//'考虑给book加一个id属性,以区别每次增加的纪录
myElement1 = doc.CreateElement("book");// '新添加的结点名称固定:book
myElement2 = doc.CreateElement("title"); //'新添加的结点名称固定:title
myElement3 = doc.CreateElement("name"); //'新添加的结点名称固定:name
myElement4 = doc.CreateElement("price");// '新添加的结点名称固定:price
// 'myElement1 = doc.CreateElement(TextBox1.Text);'新添加的结点名称由文本框给出
// 'booka = root.ChildNodes(0).ChildNodes [0]
booka = root.LastChild ;//'文档根的最后一个子结点
// ' booka = root.SelectSingleNode("descendant::book[title='" + txtBookTitle.Text + "']") ' ok:按照名称title的值: txtBookTitle.Text文本内容定位结点()
booka.AppendChild(myElement1);// 'ok:在booka的位置添加myElement1制定的结点
// 'myElement1.InnerText = "new Element text " ' ok:这是book结点的文本值"
myElement1.AppendChild(myElement2);// '为 myElement1添加子结点 myElement2:title
myElement2.InnerText = textBox1.Text;// '为子结点 myElement2:title赋值: TextBox1.Text
myElement1.AppendChild(myElement3);
myElement3.InnerText = textBox3.Text ;
myElement1.AppendChild(myElement4);
myElement4.InnerText = textBox2.Text;
// '为price添加priceType属性,并给值:"人民币"
bookc = (XmlElement)(myElement4);
bookc.SetAttribute("priceType",textBox4.Text);
}
// 'bookb = booka.FirstChild
// ' myElement1.CloneNode(True)
// ' priceNode = booka.FirstChild() 'ok:定位在booka的第一个子节点上
//
// 'booka.CloneNode(1) 'err:不可用
//
// ' booka.AppendChild(myElement1) 'ok :在当前结点的子结点之最后添加一个新的节点:myElement1
catch
{
MessageBox.Show("友情提示:", "你输入的删除条件不存在!", MessageBoxButtons.OK);
}
doc.Save(fileName);
StreamReader reader =new StreamReader(fileName);
txtDocument.Text = reader.ReadToEnd();
reader.Close();
}
private void button7_Click(object sender, System.EventArgs e)
{
// Process.Start("IExplore.exe", @"F:\j教学\xml\XML教学\xml教学05-9\8-xml.net\test5-XSLTxml\bin\转换后的文件名.htm");
XmlNode book , priceNode ;
XmlNode root = doc.DocumentElement;
try
{ //'定位到满足删除条件的结点:book上
book = root.SelectSingleNode("descendant::book[title='" + deleTextBox.Text + "']");
book.RemoveChild(book.LastChild) ;//'删除本组最后一个节点
// 'display new prices
doc.Save(fileName);
StreamReader reader = new StreamReader(fileName);
txtDocument.Text = reader.ReadToEnd();
reader.Close();
}
catch
{
MessageBox.Show("友情提示:", "你输入的删除条件不存在!",MessageBoxButtons.OK);
}
}
private void button8_Click(object sender, System.EventArgs e)
{
XmlNode book,booka;
XmlElement bookb;
XmlElement root = doc.DocumentElement;
try
{
// '定位到满足删除条件的结点:book上
book = root.SelectSingleNode("descendant::book[title='" + deleTextBox.Text + "']");
bookb = (XmlElement)book.ChildNodes[2];
bookb.RemoveAttribute("priceType", "");
// 'display new prices
doc.Save(fileName);
StreamReader reader = new StreamReader(fileName);
txtDocument.Text = reader.ReadToEnd();
reader.Close();
}
catch(Exception e1 )
{
MessageBox.Show(e1.ToString(), "你输入的删除条件不存在!",MessageBoxButtons.OK);
}
}
private void button4_Click(object sender, System.EventArgs e)
{
XmlNode book;
XmlNode root= doc.DocumentElement;
try
{
// '定位到满足删除条件的结点:book上
book = root.SelectSingleNode("descendant::book[title='" + deleTextBox.Text + "']");
book.RemoveAll() ;//'删除book的所有子结点
//'display new prices
doc.Save(fileName);
StreamReader reader = new StreamReader(fileName);
txtDocument.Text = reader.ReadToEnd();
reader.Close();
}
catch
{
MessageBox.Show("友情提示:","你输入的删除条件不存在!,", MessageBoxButtons.OK);
// 'e1.ToString()
}
}
private void xsltButton_Click(object sender, System.EventArgs e)
{
XPathDocument InputDoc=new XPathDocument(fileName);
// ' Create a new XslTransform object
XslTransform Transformer = new XslTransform ();
try
{
// ' Load a style sheet into the XslTransform object
Transformer.Load("5.15.xsl");
// ' Create a file stream to contain the results
string ObjFile = textBox5.Text + ".htm";
StreamWriter SW= new StreamWriter(ObjFile);
// ' Create an XmlTextWriter for output
Stream MS = new MemoryStream();
XmlTextWriter Xtw =new XmlTextWriter(MS, null);
// ' Transform the data
Transformer.Transform(InputDoc, null, SW);
Xtw.Close();
SW.Close();
MessageBox.Show("转换后的文件名为<" + ObjFile + ">", "文件已经转换完成!", MessageBoxButtons.OK);
}
catch(Exception e1 )
{
MessageBox.Show("当处理这个样式表单时出错!" + e1.Message, "出错误了!");
}
}
private void seekbutton_Click(object sender, System.EventArgs e)
{
XmlNode book, priceNode;
XmlNode root = doc.DocumentElement;
XmlElement bookc ;
try
{
// 'Point to the book to modify
book = root.SelectSingleNode("descendant::book[title='" + txtBookTitle.Text + "']");
bookc =(XmlElement)(book.ChildNodes[2]);
cxLabel.Text = "Tital:" + book.ChildNodes[0].InnerText + ", Name:" + book.ChildNodes[1].InnerText + ", Price:" + book.ChildNodes[2].InnerText;
if (bookc.HasAttribute("priceType"))
{
cxLabel.Text = cxLabel.Text + " priceType:" + bookc.GetAttribute("priceType");
}
}
catch
{
MessageBox.Show("你输入的《名称》不存在!重新输入!", "警告:", MessageBoxButtons.OK);
}
}
private void textBox5_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
textBox5.Text = "";
}
private void txtNewPrice_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
txtNewPrice.Text = "";
}
private void txtBookTitle_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
txtBookTitle.Text = "";
}
}
}