/*--===------------------------------------------===---
XML序列号和反序列化:将类转换为xml文件/将文件转换为xml类
许明会 2007年12月13日 21:59:19
--===------------------------------------------===---*/
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Data;
namespace xumh
{
public class Dog
{
public void Attach()
{
Console.WriteLine("Dog Attach .");
}
};
public class runMyApp
{
static void Main()
{
XmlSerializer ser = new XmlSerializer(typeof(DataSet));
//创建DataSet,DataTable,DataRow
DataSet ds = new DataSet("myDataSet");
DataTable dt = new DataTable("Table1");
dt.Columns.Add("things",typeof(string));
ds.Tables.Add(dt);
for(int i=0; i<10; i++)
{
DataRow dr = dt.NewRow();
dr[0] = "Things " + i;
dt.Rows.Add(dr);
}
//序列化
StreamWriter writer = new StreamWriter(@".\dsSerialize.xml");
ser.Serialize(writer,ds);
writer.Close();
//反序列化,将文件转换为类,然后便利数据
StreamReader reader = new StreamReader(@".\dsSerialize.xml");
DataSet dataSet = (DataSet) ser.Deserialize(reader.BaseStream);
foreach(DataRow dr in dataSet.Tables[0].Rows)
for(int i=0; i< dataSet.Tables[0].Columns.Count; i++)
Console.WriteLine("{0}", dr[0]);
//--===------------------------------------------===---
//1、要序列化的类型是什么,存到哪里? 是Dog类型
XmlSerializer se = new XmlSerializer(typeof(Dog));
StreamWriter sw = new StreamWriter(@".\DogClass.xml");
//2、序列化,将Dog类的对象dog转换为Xml文件
Dog dog = new Dog();
se.Serialize(sw,dog);
sw.Close();
//---------反序列化,将xml文件转换为Dog类的实例------------
StreamReader sr = new StreamReader(@".\DogClass.xml");
Dog d = (Dog) se.Deserialize(sr.BaseStream);
d.Attach();
}
};
}
XML序列号和反序列化:将类转换为xml文件/将文件转换为xml类
许明会 2007年12月13日 21:59:19
--===------------------------------------------===---*/
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Data;
namespace xumh
{
public class Dog
{
public void Attach()
{
Console.WriteLine("Dog Attach .");
}
};
public class runMyApp
{
static void Main()
{
XmlSerializer ser = new XmlSerializer(typeof(DataSet));
//创建DataSet,DataTable,DataRow
DataSet ds = new DataSet("myDataSet");
DataTable dt = new DataTable("Table1");
dt.Columns.Add("things",typeof(string));
ds.Tables.Add(dt);
for(int i=0; i<10; i++)
{
DataRow dr = dt.NewRow();
dr[0] = "Things " + i;
dt.Rows.Add(dr);
}
//序列化
StreamWriter writer = new StreamWriter(@".\dsSerialize.xml");
ser.Serialize(writer,ds);
writer.Close();
//反序列化,将文件转换为类,然后便利数据
StreamReader reader = new StreamReader(@".\dsSerialize.xml");
DataSet dataSet = (DataSet) ser.Deserialize(reader.BaseStream);
foreach(DataRow dr in dataSet.Tables[0].Rows)
for(int i=0; i< dataSet.Tables[0].Columns.Count; i++)
Console.WriteLine("{0}", dr[0]);
//--===------------------------------------------===---
//1、要序列化的类型是什么,存到哪里? 是Dog类型
XmlSerializer se = new XmlSerializer(typeof(Dog));
StreamWriter sw = new StreamWriter(@".\DogClass.xml");
//2、序列化,将Dog类的对象dog转换为Xml文件
Dog dog = new Dog();
se.Serialize(sw,dog);
sw.Close();
//---------反序列化,将xml文件转换为Dog类的实例------------
StreamReader sr = new StreamReader(@".\DogClass.xml");
Dog d = (Dog) se.Deserialize(sr.BaseStream);
d.Attach();
}
};
}