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

推荐订阅源

Know Your Adversary
Know Your Adversary
小众软件
小众软件
L
LangChain Blog
月光博客
月光博客
博客园 - Franky
Microsoft Azure Blog
Microsoft Azure Blog
Y
Y Combinator Blog
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MongoDB | Blog
MongoDB | Blog
Recorded Future
Recorded Future
V
Visual Studio Blog
TaoSecurity Blog
TaoSecurity Blog
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
L
LINUX DO - 热门话题
C
Check Point Blog
F
Fortinet All Blogs
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
博客园 - 司徒正美
T
Threatpost
P
Palo Alto Networks Blog
A
About on SuperTechFans
Spread Privacy
Spread Privacy
Engineering at Meta
Engineering at Meta
N
News | PayPal Newsroom
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
Blog — PlanetScale
Blog — PlanetScale
A
Arctic Wolf
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 聂微东
Google Online Security Blog
Google Online Security Blog
Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Vulnerabilities – Threatpost
H
Hacker News: Front Page

博客园 - 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 - 博客园 XmlSerializer 常见问题疑难解答(MSDN) TransactionScope和分布式事务 (转) 对象序列化:使用System.Xml.Serialization命名空间(转)
在.NET中实现对象序列化(转)
C#初学者009 · 2009-06-14 · via 博客园 - C#初学者009

       自从有了XML以后,跨系统的数据交换变得越来越简单。.NET更把XML做为底层数据交换的基础,并在.NET框架中提供了强大的类库以增强XML的编程能力。这些类包含在System.Xml命名空间中。在跨系统的数据交换中,对象的序列化与反序列化是重中之重。.NET为此专门提供了一个System.Xml.Serialization命名空间用于对象的序列化。

  要了解XML的序列化可首先参考微软的介绍:
  ● Introducing XML Serialization
  ● XML and SOAP Serialization
  ● Roadmap for XML Serialization in the .NET Framework

   .NET通过使用特性Attribute控制对象的序列化。例如:

<XmlType("order")> _
Public Class Order

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

    
<XmlArray("items"), XmlArrayItem("item")> _
    
Public OrderItems() As OrderedItem

End Class


Public Class OrderItem
    
<XmlElement("name")> _
    
Public ItemName As String
End Class


  被.NET序列化后的XML是:

<order>
    
<id>20050405</id>
    
<items>
        
<item>
            
<name>对象序列化</name>
        
</item>
    
</items>
</order>

  XmlType:可以应用于声明为Public的类、结构、枚举和接口。它定义了Xml类型的名字和命名空间。
  XmlElement:它定义了要序列化为XML元素的字段或属性。字段或属性必须声明为Public。
  XmlArray:它定义了要序列化为XML集合的字段或属性。字段或属性必须声明为Public。
  XmlArrayItem:定义了可以添加到集合中的派生类型。

  .NET还提供了其它的一些Attribute。全部的Attribut特性可以通过下面地址查看:
  ● Attributes That Control XML Serialization 
  如何使用这些Attribute,请查看:
  ● Controlling XML Serialization Using Attributes 

  有了这些Attribute,我们可以很容易的将对象序列化,但是由谁来完成呢?那就需要了解一下XmlSerializer了。ASP.NET 就是使用 XmlSerializer 类对 XML Web services 消息进行编码的。

  关于XmlSerializer 类请查看:
  ● XmlSerializer 类 
  ● XmlSerializer.Serialize 方法

   XmlSerializer的使用也非常简单:

        Dim serializer As New XmlSerializer(GetType(Order))
        
Dim writer As New StreamWriter(filename)
        
Dim o As New Order

        o.ID 
= “20050405
        
        
Dim item As OrderItem
        item.ItemName 
= “对象序列化”
        
        
Dim items(0As OrderItem
        items(
0= item
        
        o.OrderItems 
= items

        serializer.Serialize(writer, po)
        writer.Close()