惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - zsi

在线程中调用SaveFileDialog DSOFramer 之一:在 64 位系统注册 DSOFramer GridView 绑定数据不满一页时填充空行的方法 GridView 始终显示 Pager 分页行的一种方法 Chrome: Google加入浏览器大战之兼容性 调用unrar.dll时SEHException外部组件异常的处理 ASP.NET 2.0无法打开到 SQL Server 的连接 扯扯OpenFileDialog和.NET的缺省目录 给ASP.NET程序换换地儿 对象序列化:经验小结 对象序列化:使用XmlSerializer走完最后一步 在.NET中实现对象序列化 了解HTTP协议一些有用资料 Yahoo!十岁! 在VB.NET中处理构造函数时值得注意的两个陈述 微软新发布的共享设计模式的WIKI 还不快进入Design Pattern的世界? 另人费解的IsNot关键字 也说金山词霸2005内存泄露的问题
对象序列化:使用System.Xml.Serialization命名空间
zsi · 2005-04-11 · via 博客园 - zsi

   要使用.NET进行对象的序列化,必须在解决方案中添加System.Xml的引用,并且在类文件中引入System.Xml.Serialization命名空间。这样就可以在文件中使用序列化所需要的各种特性了。

Imports System.Xml.Serialization

  如果对XML Serialization缺少了解,请首先参考拙文:在.NET中实现对象序列化

   上面的例子包含了典型的XML中常见的各种元素:XML声明、XML根节点、XML节点、XML属性、XML集合。除XML声明外,在.NET中都有对应的特性用于定义这些元素。这些特性包括:XmlRootAttribute、XmlTypeAttribute、XmlElementAttribute、XmlAttributeAttribute、XmlArrayAttribute和XmlArrayItemAttribute。另外,还有两个常用的特性,XmlIgnoreAttribute用于标记在对象序列化时需要被忽略的部分,XmlIncludeAttribute用于标记在生成XML Schema时需要包括的类型。

   如果没有显式地标记任何特性,那么默认类的特性为XmlTypeAttribute、类成员的特性为XmlElementAttribute,且名称为类或类成员的名称。例如:

  如果不做任何特性标记,使用下面的代码序列化时:

  序列化后的XML为:

  可以看到,<Order>对应Order类,而<ID>和<OrderDate>分别对应Order类中的字段ID和OrderDate。另外,多了一个XML声明和两个XML命名空间。
  XML声明是.NET自动添加的,但是encoding是在XmlTextWriter中指定的,如果不指定encoding,那么XML声明只有<?xml version="1.0"?>。我使用的是.NET 1.1,这个版本中只支持XML 1.0版本。另外,如果不指定encoding,那么默认的编码可能也是UTF8(没找到相关的资料)。
  .NET默认为Order类添加了XMLSchema和XMLSchema-instance两个W3C的命名空间。该命名空间也可以自己指定,方法是使用XmlSerializer的另一个Serialize方法。

  要将类序列化为XML节点:

  要将类序列化为XML根节点:

  当在类中同时使用XmlRootAttribute、XmlTypeAttribute时,序列化文档中的类型以XmlRootAttribute为准:

  要将类成员序列化为XML节点:

        <XmlAttributeAttribute("id")> _
        
Public ID As String

  要将类成员序列化为XML属性:

        <XmlAttributeAttribute("id")> _
        
Public ID As String

  要将类成员序列化为XML集合:

  使用特性的一个好处是:可以在代码和序列化的文档中使用不同的编码规范