标签:
[Serializable] //必须添加序列化特性
public class Person
{
private string Name;//姓名
private bool Sex;//性别,是否是男
public Person(string name, bool sex)
{
this.Name = name;
this.Sex = sex;
}
public override string ToString()
{
return "姓名:" + this.Name + "\t性别:" + (this.Sex ? "男" : "女");
}
}
[Serializable] //必须添加序列化特性
public class Programmer : Person
{
private string Language;//编程语言
public Programmer(string name, bool sex, string language) : base(name, sex)
{
this.Language = language;
}
public override string ToString()
{
return base.ToString() + "\t编程语言:" + this.Language;
}
}
class Program
{
static void Main(string[] args)
{
//创建Programmer列表,并添加对象
List<Programmer> list = new List<Programmer>();
list.Add(new Programmer("李志伟", true, "C#"));
list.Add(new Programmer("Coder2", false, "C++"));
list.Add(new Programmer("Coder3", true, "Java"));
//使用二进制序列化对象
string fileName = @"D:\users\lizw\桌面\Programmers.dat";//文件名称与路径
Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器
binFormat.Serialize(fStream, list);
//使用二进制反序列化对象
list.Clear();//清空列表
fStream.Position = 0;//重置流位置
list = (List<Programmer>)binFormat.Deserialize(fStream);//反序列化对象
foreach (Programmer p in list)
{
Console.WriteLine(p);
}
Console.Read();
}
}
[Serializable] //必须添加序列化特性
public class Person
{
private string Name;//姓名
private bool Sex;//性别,是否是男
public Person(string name, bool sex)
{
this.Name = name;
this.Sex = sex;
}
public override string ToString()
{
return "姓名:" + this.Name + "\t性别:" + (this.Sex ? "男" : "女");
}
}
[Serializable] //必须添加序列化特性
public class Programmer : Person
{
private string Language;//编程语言
public Programmer(string name, bool sex, string language) : base(name, sex)
{
this.Language = language;
}
public override string ToString()
{
return base.ToString() + "\t编程语言:" + this.Language;
}
}
class Program
{
static void Main(string[] args)
{
//实例化对象
Programmer p = new Programmer("李志伟", true, "C、C#、C++、Java");
//使用SOAP序列化对象
string fileName = @"D:\users\lizw\桌面\Programmers.xml";//文件名称与路径
Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
SoapFormatter soapFormat = new SoapFormatter();//创建SOAP序列化器
soapFormat.Serialize(fStream, p);//SOAP不能序列化泛型对象
//使用SOAP反序列化对象
fStream.Position = 0;//重置流位置
p = null;
p = (Programmer)soapFormat.Deserialize(fStream);
Console.WriteLine(p);
Console.Read();
}
}
public class Person
{
public string Name;//姓名
public bool Sex;//性别,是否是男
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
public Person(string name, bool sex)
{
this.Name = name;
this.Sex = sex;
}
public override string ToString()
{
return "姓名:" + this.Name + "\t性别:" + (this.Sex ? "男" : "女");
}
}
public class Programmer : Person
{
public string Language;//编程语言
public Programmer() { }//必须提供无参构造器,否则XmlSerializer将出错
public Programmer(string name, bool sex, string language) : base(name, sex)
{
this.Language = language;
}
public override string ToString()
{
return base.ToString() + "\t编程语言:" + this.Language;
}
}
class Program
{
static void Main(string[] args)
{
//创建Programmer列表,并添加对象
List<Programmer> list = new List<Programmer>();
list.Add(new Programmer("李志伟", true, "C#"));
list.Add(new Programmer("Coder2", false, "C++"));
list.Add(new Programmer("Coder3", true, "Java"));
//使用XML序列化对象
string fileName = @"D:\users\lizw\桌面\Programmers.xml";//文件名称与路径
Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
XmlSerializer xmlFormat = new XmlSerializer(
typeof(List<Programmer>),
new Type[] { typeof(Programmer),typeof(Person) });//创建XML序列化器,需要指定对象的类型
xmlFormat.Serialize(fStream, list);
//使用XML反序列化对象
fStream.Position = 0;//重置流位置
list.Clear();
list = (List<Programmer>)xmlFormat.Deserialize(fStream);
foreach (Programmer p in list)
{
Console.WriteLine(p);
}
Console.Read();
}
}
public class Person
{
[XmlElement]
public string Name;//使用[XmlElement]特性
public bool Sex;//默认使用了[XmlElement]特性
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
}
<Person xmlns:xsi="..." xmlns:xsd="...">
<Name>李志伟</Name>
<Sex>true</Sex>
</Person>
public class Person
{
[XmlElement]
public string Name;
[XmlAttribute]
public bool Sex;
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
}
<Person xmlns:xsi="..." xmlns:xsd="..." Sex="true">
<Name>李志伟</Name>
</Person>
public class Person
{
[XmlText]
public string Name;
[XmlAttribute]
public bool Sex;
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
}
<Person xmlns:xsi="..." xmlns:xsd="..." Sex="true">李志伟</Person>
[XmlType("个人信息")]
public class Person
{
[XmlAttribute("姓名")]
public string Name;
[XmlAttribute("性别")]
public bool Sex;
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
}
<个人信息 xmlns:xsi="..." xmlns:xsd="..." 姓名="李志伟" 性别="true" />
[XmlType("个人信息")]
public class Person
{
[XmlAttribute("姓名")]
public string Name;
[XmlAttribute("性别")]
public bool Sex;
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
}
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Name = "李志伟";
p.Sex = true;
Person[] ps = new Person[3];
ps[0] = p;
ps[1] = p;
ps[2] = p;
//使用XML序列化对象
string fileName = @"D:\users\lizw\桌面\Programmers.xml";//文件名称与路径
Stream fStream = new FileStream(fileName, FileMode.Create);
XmlSerializer xmlFormat = new XmlSerializer(typeof(Person[]));
xmlFormat.Serialize(fStream, ps);//序列化对象
fStream.Dispose();//关闭文件
Console.WriteLine("OK!");
Console.Read();
}
}
<ArrayOf个人信息 xmlns:xsi="..." xmlns:xsd="...">
<个人信息 姓名="李志伟" 性别="true" />
<个人信息 姓名="李志伟" 性别="true" />
<个人信息 姓名="李志伟" 性别="true" />
</ArrayOf个人信息>
[XmlType("个人信息")]
public class Person
{
[XmlAttribute("姓名")]
public string Name;
[XmlAttribute("性别")]
public bool Sex;
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
}
[XmlType("人员信息")]
public class PersonArray : List<Person> { }
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Name = "李志伟";
p.Sex = true;
PersonArray ps = new PersonArray();
ps.Add(p);
ps.Add(p);
ps.Add(p);
//使用XML序列化对象
string fileName = @"D:\users\lizw\桌面\Programmers.xml";//文件名称与路径
Stream fStream = new FileStream(fileName, FileMode.Create);
XmlSerializer xmlFormat = new XmlSerializer(typeof(PersonArray));
xmlFormat.Serialize(fStream, ps);//序列化对象
fStream.Dispose();//关闭文件
Console.WriteLine("OK!");
Console.Read();
}
}
<人员信息 xmlns:xsi="..." xmlns:xsd="...">
<个人信息 姓名="李志伟" 性别="true" />
<个人信息 姓名="李志伟" 性别="true" />
<个人信息 姓名="李志伟" 性别="true" />
</人员信息>
[XmlType("信息")]
public class Person
{
[XmlAttribute("姓名")]
public string Name;
[XmlAttribute("性别")]
public bool Sex;
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
}
public class PersonArray
{
public List<Person> Array=new List<Person>();
public Person Person = new Person();
}
class Program
{
static void Main(string[] args)
{
PersonArray ps = new PersonArray();
ps.Person = new Person();
ps.Person.Name = "李志伟";
ps.Person.Sex = true;
ps.Array.Add(ps.Person);
ps.Array.Add(ps.Person);
ps.Array.Add(ps.Person);
//使用XML序列化对象
string fileName = @"D:\users\lizw\桌面\Programmers.xml";//文件名称与路径
Stream fStream = new FileStream(fileName, FileMode.Create);
XmlSerializer xmlFormat = new XmlSerializer(typeof(PersonArray));
xmlFormat.Serialize(fStream, ps);//序列化对象
fStream.Dispose();//关闭文件
Console.WriteLine("OK!");
Console.Read();
}
}
<PersonArray xmlns:xsi="..." xmlns:xsd="...">
<Array>
<个人信息 姓名="李志伟" 性别="true" />
<个人信息 姓名="李志伟" 性别="true" />
<个人信息 姓名="李志伟" 性别="true" />
</Array>
<Person 姓名="李志伟" 性别="true" />
</PersonArray>
[XmlType("信息")]
public class Person
{
[XmlAttribute("姓名")]
public string Name;
[XmlAttribute("性别")]
public bool Sex;
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
}
public class PersonArray
{
[XmlArrayItem("个人信息")]
[XmlArray("人员信息")]
public List<Person> Array=new List<Person>();
public Person Person = new Person();
}
<PersonArray xmlns:xsi="..." xmlns:xsd="...">
<人员信息>
<个人信息 姓名="李志伟" 性别="true" />
<个人信息 姓名="李志伟" 性别="true" />
<个人信息 姓名="李志伟" 性别="true" />
</人员信息>
<Person 姓名="李志伟" 性别="true" />
</PersonArray>
[XmlType("信息")]
public class Person
{
[XmlAttribute("姓名")]
public string Name;
[XmlAttribute("性别")]
public bool Sex;
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
}
public class PersonArray
{
[XmlElement("个人信息")]
public List<Person> Array=new List<Person>();
public Person Person = new Person();
}
<PersonArray xmlns:xsi="..." xmlns:xsd="...">
<个人信息 姓名="李志伟" 性别="true" />
<个人信息 姓名="李志伟" 性别="true" />
<个人信息 姓名="李志伟" 性别="true" />
<Person 姓名="李志伟" 性别="true" />
</PersonArray>
public class Base { }
[XmlType("信息A")]
public class PersonA : Base
{
[XmlAttribute("姓名")]
public string Name;
[XmlAttribute("性别")]
public bool Sex;
public PersonA() { }//必须提供无参构造器,否则XmlSerializer将出错
}
[XmlType("信息B")]
public class PersonB : Base
{
[XmlElement("姓名")]
public string Name;
[XmlElement("年龄")]
public int Age;
public PersonB() { }//必须提供无参构造器,否则XmlSerializer将出错
}
[XmlType("人员信息")]
public class PersonArray
{
[XmlArrayItem(typeof(PersonA)), XmlArrayItem(typeof(PersonB))]
public List<Base> ListPerson=new List<Base>();
}
class Program
{
static void Main(string[] args)
{
PersonA pa = new PersonA();
pa.Name = "李志伟A";
pa.Sex = true;
PersonB pb = new PersonB();
pb.Name = "李志伟B";
pb.Age = 21;
PersonArray ps = new PersonArray();
ps.ListPerson.Add(pa);
ps.ListPerson.Add(pa);
ps.ListPerson.Add(pb);
ps.ListPerson.Add(pb);
//使用XML序列化对象
string fileName = @"D:\users\lizw\桌面\Programmers.xml";//文件名称与路径
Stream fStream = new FileStream(fileName, FileMode.Create);
XmlSerializer xmlFormat = new XmlSerializer(typeof(PersonArray));
xmlFormat.Serialize(fStream, ps);//序列化对象
fStream.Dispose();//关闭文件
Console.WriteLine("OK!");
Console.Read();
}
}
<人员信息 xmlns:xsi="..." xmlns:xsd="...">
<ListPerson>
<信息A 姓名="李志伟A" 性别="true" />
<信息A 姓名="李志伟A" 性别="true" />
<信息B>
<姓名>李志伟B</姓名>
<年龄>21</年龄>
</信息B>
<信息B>
<姓名>李志伟B</姓名>
<年龄>21</年龄>
</信息B>
</ListPerson>
</人员信息>
public class Person
{
public string Name;
[XmlIgnore]// 这个属性将不会参与序列化
public bool Sex;
public Person() { }
}
<Person xmlns:xsi="..." xmlns:xsd="...">
<Name>李志伟</Name>
</Person>
public class Person
{
[XmlElement(Order = 2)]
public string Name;
[XmlElement(Order = 1)]
public bool Sex;
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
}
<Person xmlns:xsi="..." xmlns:xsd="...">
<Sex>true</Sex>
<Name>李志伟</Name>
</Person>
public class Person : IXmlSerializable
{
public string Name;
public bool Sex;
public Person() { }//必须提供无参构造器,否则XmlSerializer将出错
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
Name = reader.GetAttribute("姓名");
Sex = reader.GetAttribute("性别").Equals("男") ? true : false;
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("姓名", Name);
writer.WriteAttributeString("性别", Sex ? "男" : "女");
}
}
<Person 姓名="李志伟" 性别="男" />
[XmlRoot(Namespace = "http://msdn.microsoft.com/vsdh.xsd")]
public class Person
{
public string Name;
public bool Sex;
public Person() { }
}
<Person xmlns:xsi="..." xmlns:xsd="..." xmlns="http://msdn.microsoft.com/vsdh.xsd">
<Name>李志伟A</Name>
<Sex>true</Sex>
</Person>
[Serializable]
public class Person : ISerializable
{
public string Name;
public bool Sex;
public Person() { }
//必须的够着方法,反序列化时调用
protected Person(SerializationInfo info, StreamingContext context)
{
Name = info.GetString("姓名");
Sex = info.GetBoolean("性别");
}
//序列化时调用
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("姓名", Name + "(自定义序列化)");
info.AddValue("性别", Sex);
}
public override string ToString()
{
return "姓名:" + this.Name + "\t性别:" + (this.Sex ? "男" : "女");
}
}
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Name = "李志伟A";
p.Sex = true;
//使用二进制序列化对象
string fileName = @"D:\users\lizw\桌面\Programmers.xml";//文件名称与路径
Stream fStream = new FileStream(fileName, FileMode.Create);
BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器
binFormat.Serialize(fStream, p);//序列化对象
//使用二进制反序列化对象
fStream.Position = 0;//重置流位置
p = (Person)binFormat.Deserialize(fStream);//反序列化对象
Console.WriteLine(p);
fStream.Dispose();//关闭文件
Console.WriteLine("OK!");
Console.Read();
}
}
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4316650.html