您是第 Web Page Tracking 位访客

水~墨~

昂首阔步,不留一点遗憾!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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;
}

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();
}

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);

 

生成的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>

 

 

 

 

 

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

 

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();

}


posted on 2013-03-07 15:54  水墨.MR.H  阅读(1132)  评论(0编辑  收藏  举报
知识共享许可协议
本博客为水墨原创,基于Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的水墨(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。