您的位置:首页 > 其它

XML序列化和反序列化及实例

2013-03-07 15:54 253 查看
XmlSerializer.Deserialize 方法 (XmlReader) 反序列化

1.普通xml序列化,不带属性序列化

2.带属性的序列化 SoapAttribute 属性 (本章主要代码)

*****************************************************************************************************

关键1:[SoapAttribute(DataType = "date", AttributeName = "CreationDate")] //此句告诉编译器 序列化成属性,然后属性name为createiondate

public DateTime Today;

关键2: 带属性的序列化和反序列化要使用下面的

XmlTypeMapping myMapping = (new SoapReflectionImporter().ImportTypeMapping(typeof(T)));

XmlSerializer mySerializer = new XmlSerializer(myMapping);

*******************************************************************************************************

1.基本类

public class Douban
{
public Logininfo Login=new Logininfo();
public Registinfo Regist=new Registinfo();
}

public class Logininfo
{

[SoapAttribute(DataType = "string", AttributeName = "CreationDate")]
public string testValue;

public  string LoginUrl;
public  string LoginCodeUrl;
}
public class Registinfo
{
public  string RegistUrl;
public  string RegistCodeUrl;
}

[/code]
2.序列化的方法

public void XmlSerialize<T>(string filename,T t)
{
// Create an instance of the XmlSerializer class.
XmlTypeMapping myMapping =
(new SoapReflectionImporter().ImportTypeMapping(
typeof(T)));
XmlSerializer mySerializer =
new XmlSerializer(myMapping);
XmlTextWriter writer =
new XmlTextWriter(filename, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("wrapper");   //词句生成必须有,代表最顶级的元素名称
// Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, t);
writer.WriteEndElement();
writer.Close();
}

[/code]
3. 使用序列化方法,序列化指定的类

生成douban实例d,并在 Login字段上增加属性CreationDate= wocaonima

Douban d = new Douban();
d.Login.testValue = "wocaonima";
d.Login.LoginUrl = "1";
d.Login.LoginCodeUrl = "2";
d.Regist.RegistUrl = "3";
d.Regist.RegistCodeUrl = "4";

XmlSerialize<Douban>(@"C:\Users\ipod\Documents\Visual Studio 2010\Projects\douban注册_2013.3.4_DataGridView\datagridvew 实例\datagridvew 实例\ini3.xml", d);

[/code]

生成的ini3.xml 文件内容如下。

<wrapper>
<Douban xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1">
<Login href="#id2" />
<Regist href="#id3" />
</Douban>
<Logininfo id="id2" d2p1:type="Logininfo" CreationDate="wocaonima" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance">
<LoginUrl xmlns:q1="http://www.w3.org/2001/XMLSchema" d2p1:type="q1:string">1</LoginUrl>
<LoginCodeUrl xmlns:q2="http://www.w3.org/2001/XMLSchema" d2p1:type="q2:string">2</LoginCodeUrl>
</Logininfo>
<Registinfo id="id3" d2p1:type="Registinfo" xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance">
<RegistUrl xmlns:q3="http://www.w3.org/2001/XMLSchema" d2p1:type="q3:string">3</RegistUrl>
<RegistCodeUrl xmlns:q4="http://www.w3.org/2001/XMLSchema" d2p1:type="q4:string">4</RegistCodeUrl>
</Registinfo>
</wrapper>

[/code]

最后补充带属性的反序列化方法:

public void DeserializeOriginal<T>(string filename,T t)
{
// Create an instance of the XmlSerializer class.
XmlTypeMapping myMapping =
(new SoapReflectionImporter().ImportTypeMapping(
typeof(T)));
XmlSerializer mySerializer =
new XmlSerializer(myMapping);

// Reading the file requires an  XmlTextReader.
XmlTextReader reader=
new XmlTextReader(filename);
reader.ReadStartElement("wrapper");

// Deserialize and cast the object.

myGroup = (t) mySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.Close();

}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: