已知有一个XML文件(bookstore.xml)如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>
1、往<bookstore>节点中插入一个<book>节点:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<book>节点中
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<bookstore>节点中
xmlDoc.Save("bookstore.xml");
//===============================================
结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>
2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
{
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亚胜";//则修改
break;//找到退出来就可以了
}
}
break;
}
}
xmlDoc.Save("bookstore.xml");//保存。
//==================================================
最后结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="update李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price>
</book>
</bookstore>
3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。
XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
else if(xe.GetAttribute("genre")=="update李赞红")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookstore.xml");
//===========================================
最后结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>
4、显示所有数据。
XmlNode xn=xmlDoc.SelectSingleNode("bookstore");
XmlNodeList xnl=xn.ChildNodes;
foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}
}
2010年5月5日星期三
[+/-] |
c#读写XML文件 |
[+/-] |
利用委托和对象处理窗体间传值 |
今天在网上找关于窗体间传值的方法,看了好几个,但感觉两个比较好用,现转过来以便以后方便使用
1.窗体间以对象的形式传递
假设有两个form,form1、form2和一个Class1.cs的类文件。form1是程序的开始窗体,通过form1 来调用form2 。程序如下:
Class1.cs文件的内容是
public class Class1
{
public int i;
public Class1()
{
//
// TODO:
i = 9;
}
public void modify( int u )
{
i = u;
}
}
Form1中的内容是
private Class1 ttt;
private void Form1_Load(object sender, System.EventArgs e)
{
ttt = new Class1();
Form2 temp = new Form2();
temp.Change = ttt;
temp.Show();
}
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text = ttt.i.ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{
ttt.modify( 44);
}
form2中的内容是:
private Class1 change;
public Class1 Change
{
get { return change ;}
set
{
change= value;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text = change.i.ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{
change.modify( 98 );
}
运行程序你会发现,改form1 中的textbox值,那么form2 中change中的i 的值也会相应的变,同样form2中change的 i 值变了,那么form1 中的 ttt 中的 i 也相应的变了。就好像两个form在使用同一个数据变量一样,为什么呢?
经过思考,其实很简单,原因就在于我们在使用property传递数据时使用了同一块内存空间。(牛牛注:我想就是值传递,因为把对象作为参数就是值传递)
在传递class类型的数据时(如上),由于我们没有new一个新的实例,而是直接赋值,所以就相当于使用了引用,把上面的赋值过程改成下面这样,
private Class1 change;
public Class1 Change
{
get { return change ;}
set
{
change = new Class1();
change.i = value.i ;
}
}
那么两个form中的值相互之间就不再有什么关联了,也就是说,改其中一个,不会影响另外一个。这也给了我们一个启示,当我们想在form之间传值时,而且还想让值之间有一定的联系,那么就把这些值用class来包起来,再传。这样又清楚又省事。
如 果你传的不是类而是一般数据(int , string )等,那么这些数据在form之间是没有联系的,因为C#在定义这些数据类型时,就默认new了他们,例如:int i ; 和int i = new int() ; 是一样的,所以我以前经常在form之间传递简单的变量时,没有发现数据之间的关系,直到今天传 class 时才发现。像下面这样的 class :
class temp
{
int i ;
int[] mm;
public temp ()
{ mm = new int [10] ; }
}
在form之间传递时,变量 i 是两个窗体共用的,但是mm 却不是,其原因就是我上面讲的那样,所以利用property的这个特性,我们可以灵活的在form之间传递我们想变和不想变的值。
在这里我要加几句,这个方法其实还有一种本质上和他一样的方法,就是把窗体作为对象进行转递来达到相同的效果,只是这样的方法比较适用于两个窗体间的互动,多个窗体就不是很好用了:
(This article describes a simplified approach to allowing communication between forms without the use of events and delegates. The approach demonstrated is adequate if the application uses a single instance of a form and allows the creation of a single instance of a dialog used to pass data to the main calling form. If you need to broadcast the data generated by a dialog to multiple form listeners, you will still need to work with events and delegates; this approach is only valid if there is a one on one relationship between the two interactive forms.)这是这个方法的一点说明:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LimitedDataXfer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenForm_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LimitedDataXfer
{
public partial class Form2 : Form
{
Form1 f;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 fr1)
{
InitializeComponent();
f = new Form1();
f = fr1;
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
f.lblName.Text = textBox1.Text;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
f.lblStreet.Text = textBox2.Text;
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
f.lblCity.Text = textBox3.Text;
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
f.lblState.Text = textBox4.Text;
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
f.lblZip.Text = textBox5.Text;
}
}
}
2. 用委托的方式传递
FROM2中
public delegate void changelabel(string s);
public event changelabel change;
private void button1_Click(object sender, System.EventArgs e)
{
if(change != null)
{
change(this.textBox1.Text);
}
}
FROM 1中
private void button2_Click(object sender, System.EventArgs e)
{
frmlabel frm = new frmlabel();
frm.change += new frmlabel.changelabel(myevent);
frm.Show();
}
private void myevent(string s)
{
this.label2.Text = s;
}
http://blog.csdn.net/simonllf/archive/2007/03/01/1517934.aspx
[+/-] |
窗体间利用属性(property)来传递值时应注意的问题。 |
这也许是个老话题了,但是今天我在用property 时却有了新的发现。那就是如果传递的是类的话,那么是按地址(引用)来传递的。例如如下两个程序。假设有两个form,form1、form2和一个 Class1.cs的类文件。form1是程序的开始窗体,通过form1 来调用form2 。程序如下:
Class1.cs文件的内容是
public class Class1
{
public int i;
public Class1()
{
//
// TODO:
i = 9;
}
public void modify( int u )
{
i = u;
}
}
Form1中的内容是
private Class1 ttt;
private void Form1_Load(object sender, System.EventArgs e)
{
ttt = new Class1();
Form2 temp = new Form2();
temp.Change = ttt;
temp.Show();
}
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text = ttt.i.ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{
ttt.modify( 44);
}
form2中的内容是:
private Class1 change;
public Class1 Change
{
get { return change ;}
set
{
change= value;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text = change.i.ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{
change.modify( 98 );
}
运行程序你会发现,改form1 中的textbox值,那么form2 中change中的i 的值也会相应的变,同样form2中change的 i 值变了,那么form1 中的 ttt 中的 i 也相应的变了。就好像两个form在使用同一个数据变量一样,为什么呢?
经过思考,其实很简单,原因就在于我们在使用property传递数据时使用了同一块内存空间。
在传递class类型的数据时(如上),由于我们没有new一个新的实例,而是直接赋值,所以就相当于使用了引用,把上面的赋值过程改成下面这样,
private Class1 change;
public Class1 Change
{
get { return change ;}
set
{
change = new Class1();
change.i = value.i ;
}
}
那么两个form中的值相互之间就不再有什么关联了,也就是说,改其中一个,不会影响另外一个。这也给了我们一个启示,当我们想在form之间传值时,而且还想让值之间有一定的联系,那么就把这些值用class来包起来,再传。这样又清楚又省事。
如 果你传的不是类而是一般数据(int , string )等,那么这些数据在form之间是没有联系的,因为C#在定义这些数据类型时,就默认new了他们,例如:int i ; 和int i = new int() ; 是一样的,所以我以前经常在form之间传递简单的变量时,没有发现数据之间的关系,直到今天传 class 时才发现。像下面这样的 class :
class temp
{
int i ;
int[] mm;
public temp ()
{ mm = new int [10] ; }
}
在form之间传递时,变量 i 是两个窗体共用的,但是mm 却不是,其原因就是我上面讲的那样,所以利用property的这个特性,我们可以灵活的在form之间传递我们想变和不想变的值。
http://blog.csdn.net/tjvictor/archive/2006/06/04/772711.aspx
[+/-] |
windows form (窗体) 之间传值小结 |
在windows form之间传值,我总结了有四个方法:全局变量、属性、窗体构造函数和delegate。
第一个全局变量:
这个最简单,只要把变量描述成static就可以了,在form2中直接引用form1的变量,代码如下:
在form1中定义一个static变量public static int i= 9 ;
Form2中的钮扣按钮如下:
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text = Form1.i.ToString();
}
第二个方法是利用属性,请详见博客:
http://blog.csdn.net/tjvictor/archive/2006/06/04/772711.aspx
第三个方法是用构造函数:
Form1 的button按钮这样写:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 temp = new Form2( 9 );
temp.Show();
}
Form2 的构造函数这样写:
public Form2( int i )
{
InitializeComponent();
textBox1.Text = i.ToString();
}
第四个方法是用delegate,代码如下:
Form2中先定义一个delegate
public delegate void returnvalue( int i );
public returnvalue ReturnValue;
form2 中的button按钮代码如下:
private void button1_Click(object sender, System.EventArgs e)
{
if ( ReturnValue != null )
ReturnValue( 8 );
}
Form1中的button按键如下:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 temp = new Form2( );
temp.ReturnValue = new temp.Form2.returnvalue( showvalue );
temp.Show();
}
private void showvalue( int i )
{
textBox1.Text = i.ToString();
}
点击form2的button,form1中的textbox中的值就会相应变化。
在这四个方法中,
第一个是双向传值,也就是说,form1和form2改变i的值,另一方也会受到影响。
第二个方法可以单向也可以双向传值。
第三个方法是form1->form2单向传值。
第四个方法是form2->form1单向传值。
以后有新的方法我再补充,还有一个就是用event,和delegate差不多,在这里就不说了。