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

推荐订阅源

P
Privacy International News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
T
Tailwind CSS Blog
WordPress大学
WordPress大学
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - Franky
C
CERT Recently Published Vulnerability Notes
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
雷峰网
雷峰网
Schneier on Security
Schneier on Security
博客园 - 聂微东
T
Tor Project blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
AI
AI
T
Troy Hunt's Blog
Security Latest
Security Latest
T
The Blog of Author Tim Ferriss
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Check Point Blog
T
Threat Research - Cisco Blogs
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
Recorded Future
Recorded Future
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cloudbric
Cloudbric
J
Java Code Geeks
罗磊的独立博客
C
Cyber Attacks, Cyber Crime and Cyber Security
aimingoo的专栏
aimingoo的专栏
L
LangChain Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
Lohrmann on Cybersecurity
I
InfoQ
MongoDB | Blog
MongoDB | Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
The Hacker News
The Hacker News
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
N
News and Events Feed by Topic

博客园 - C#初学者009

脱壳学习 如何将字符串动态转换为指定的值类型 - C#初学者009 - 博客园 SQL Server表描述 及 字段描述的增、删、改、查询 关于Wind2003下MSDTC对DTS运行的影响 SQL 标识列起始种子重设 C#跨线程操作控件 通过委托处理,MSDN上又很详细用法的说明 - C#初学者009 - 博客园 .net项目开发工具(V3.0 ) asp.net控件开发基础(5) -- 复杂属性、内嵌属性 - C#初学者009 如何用vb设置默认打印机? 如何使用 SetPrinter 修改打印机设置 用vb做的activex控件打包成cab后如何发布呢? VB中利用CopyMemory使用指针 如何用vb(API)代码设置不规则打印纸尺寸? 如何在 Windows NT 和 Windows 2000 中使用自定义页面大小打印(VB) 确定打印机状态和打印工作状态从Visual Basic VB怎么检测打印机状态 - C#初学者009 - 博客园 在.NET中实现对象序列化(转) XmlSerializer 常见问题疑难解答(MSDN) TransactionScope和分布式事务 (转)
对象序列化:使用System.Xml.Serialization命名空间(转)
C#初学者009 · 2009-06-14 · via 博客园 - C#初学者009


   上面的例子包含了典型的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集合:

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