zoukankan      html  css  js  c++  java
  • XML序列化和反序列化及实例

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

    }


  • 相关阅读:
    Git的使用
    Flask(五)
    Flask(四)
    Flask(二)
    Flask(一)
    SDL 五子棋游戏
    c++单例模式
    ubuntu安装虚拟机
    git 命令
    汇编x86入门
  • 原文地址:https://www.cnblogs.com/StudyLife/p/2948223.html
Copyright © 2011-2022 走看看