smhy8187

 

c#序列化 xml

 

要序列化的对象的类:

[Serializable]
public class Person
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
public string Sex;
public int Age=31;
public Course[] Courses;

public Person()
{
}
public Person(string Name)
{
name=Name;
Sex="男";
}
}
[Serializable]
public class Course
{
public string Name;
[XmlIgnore]public string Description;
public Course()
{
}
public Course(string name,string description)
{
Name=name;
Description=description;
}

进行序列化及反序列化的测试类:

class Test
{
//序列化
public void Serialiaze()
{
Person c=new Person("cyj")
c.Courses=new Course[2];
c.Courses[0]=new Course("英语","交流工具")
c.Courses[1]=new Course("数学","自然科学")

XmlSerializer xs=new XmlSerializer(typeof(Person));
Stream stream = new FileStream("c:\\cyj.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
xs.Serialize(stream, c);
stream.Close();
}
//反序列化
public void Deserialize()
{
XmlSerializer xs=new XmlSerializer(typeof(Person));
Stream stream = new FileStream("c:\\cyj.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Person p=(Person)xs.Deserialize(stream);
Console.WriteLine(p.Name);
Console.WriteLine(p.Age.ToString());
Console.WriteLine(p.Courses.Length.ToString());
Console.Read();
}
}

格式化后Xml的文档内容为:
<?xml version="1.0"?>
<Person xmlns:xsd=http://www.w3.org/2001/XMLSchema    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Sex>男</Sex>
  <Age>31</Age>
  <Courses>
    <Course>
      <Name>英语</Name>
    </Course>
    <Course>
      <Name>数学</Name>
    </Course>
  </Courses>
  <Name>cyj</Name>
</Person>


-----------------------------------------------------

C#中XML序列化和反序列化初探
2007-08-16 17:13

这里是一个Demo,简单实现了C#中对象的XML序列化技术。

首先,我们定义一个需要序列化的对象:

using System;

namespace XMLSerializer

{

       /// <summary>

       /// 测试类

       /// </summary>

       public class TestXML

       {

              public string name;

              public string sex;

              public string age;

       }

}

然后我们就可以在程序中使用这个类构造对象,对其序列化到XML文档。

private void button1_Click(object sender, System.EventArgs e)

{

       TestXML a = new TestXML();

       a.name = tbName.Text;

       a.sex = tbSex.Text;

       a.age = tbAge.Text;

       SaveFileDialog of = new SaveFileDialog();

       of.Filter = " XML文档|*.XML";

       if( of.ShowDialog() == DialogResult.OK )

       {

              try

              {

                     Stream s = of.OpenFile();

                     new XmlSerializer( a.GetType() ).Serialize( s, a );

                     s.Close();

              }

              catch( Exception ex )

              {

                     MessageBox.Show( ex.Message );

              }

       }

}

最后,从XML文档中反序列化出对象

private void button2_Click(object sender, System.EventArgs e)

{

       OpenFileDialog o = new OpenFileDialog();

       o.Filter = " XML文档|*.XML|所有文件|*.*";

       if( o.ShowDialog() == DialogResult.OK )

       {

              try

              {

                     XmlSerializer xs = new XmlSerializer( typeof( TestXML ) );

                     Stream s = o.OpenFile();

                     TestXML a = xs.Deserialize( s ) as TestXML;

                     

                     tbName.Text = a.name;

                     tbSex.Text = a.sex;

                     tbAge.Text = a.age;

                     s.Close();

              }

              catch( Exception ex )

              {

                     MessageBox.Show( ex.Message );

              }

       }

}


posted on 2008-04-17 21:02  new2008  阅读(1031)  评论(1编辑  收藏  举报

导航