实现C# XML序列化技术使用到什么具体的方法呢?我们在具体的操作过程中需要注意什么呢?那么这里向你展示一个Demo希望对你了解C# XML序列化技术有所帮助
首先我们定义一个需要序列化的对象
using System; namespace XMLSerializer
{
/// ﹤summary﹥
/// 测试类
/// ﹤/summary﹥
public class TestXML
{
public string name;
public string sex;
public string age;
}
}
然后我们就可以在程序中使用这个类构造对象C# XML序列化
)thisstylewidth=; border= small=>
private void button_Click( object sender SystemEventArgs e)
{
TestXML a = new TestXML();
aname = tbNameText;
asex = tbSexText;
aage = tbAgeText;
SaveFileDialog of = new SaveFileDialog();
ofFilter = XML文档|*XML;
if( ofShowDialog() == DialogResultOK )
{
try { Stream s = ofOpenFile();
new XmlSerializer( aGetType() )Serialize( s a );
sClose();
}
catch( Exception ex )
{
MessageBoxShow( exMessage );
}
}
}
C# XML序列化之从XML文档中反序列化出对象
private void button_Click(object sender SystemEventArgs e)
{
OpenFileDialog o = new OpenFileDialog();
oFilter = XML文档|*XML|所有文件|**;
if( oShowDialog() == DialogResultOK )
{
try
{
XmlSerializer xs = new XmlSerializer( typeof( TestXML )
);
Stream s = oOpenFile();
TestXML a = xsDeserialize( s ) as TestXML;
tbNameText = aname;
tbSexText = asex;
tbAgeText = aage;
sClose();
}
catch( Exception ex )
{
MessageBoxShow( exMessage );
}
}
}
C# XML序列化的具体实现实例就向你介绍到这里希望那个对你了解和学习C# XML序列化有所帮助