xml反序列化时 如何生成与之对应的类文件

作者:xiaotie  来源:网络  【打印此文】【
 (微软定义)序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。
  
  二、学习这方面的知识需要那些基础
  1,知道XML是什么,如何定义一个标准的XML文件
  2,知道XML架构(Schema)是什么,如何定义一个标准的XML架构文件
  3,知道XSD.exe的基本用法。
  如果你对XML或Schema有什么不清楚的地方,请到这里来学习 中国XML论坛
  
  三、案例学习
  假如我们这这样一个xml文件,我们需要将它反序列化为对象
  
  <?xml version="1.0" encoding="UTF-8"?>
  <!--示例 XML 文件由 XMLSpy v2006 U 创建 (http://www.altova.com)-->
  <Pages>
   <FirstPage Url="Text">
   <SecondPage Url="Text"/>
   <SecondPage Url="Text"/>
   </FirstPage>
   <FirstPage Url="Text">
   <SecondPage Url="Text"/>
   <SecondPage Url="Text"/>
   </FirstPage>
  </Pages>要将xml反序列化为对象,他们之间不能直接转换,需要一个中间人,就是Schema,Schema文件我们可以用工具生成,也可以自己手写,一般的做法就是先用工具生成,然后在其基础上修改,我这里用的生成工具是XMLSpy v2006,默认生成格式如下:
  Code
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <!--W3C Schema 由 XMLSpy v2006 U 创建 (http://www.altova.com)-->
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
   <xs:element name="FirstPage">
   <xs:complexType>
   <xs:sequence>
   <xs:element ref="SecondPage" maxOccurs="unbounded"/>
   </xs:sequence>
   <xs:attribute name="Url" use="required">
   <xs:simpleType>
   <xs:restriction base="xs:string">
   <xs:enumeration value="Text"/>
   </xs:restriction>
   </xs:simpleType>
   </xs:attribute>
   </xs:complexType>
   </xs:element>
   <xs:element name="Pages">
   <xs:complexType>
   <xs:sequence>
   <xs:element ref="FirstPage" maxOccurs="unbounded"/>
   </xs:sequence>
   </xs:complexType>
   </xs:element>
   <xs:element na

[1] [2] [3] [4] [5] [6] [7] 下页  




久尚整理     更多关于xml反序列化时 如何生成与之对应的类文件 的文章

{$Title}
搜索:

CSS样式表制作文字

不安装和修改文件

周付联盟推荐第九

QQ免费电子密保卡

QQ邮箱再更新 优化