C#语言中的XmlSerializer类的XmlSerializer.Deserialize (Stream)方法举例详解
包含由指定的 XML 文档反序列化 Stream。
命名空间: System.Xml.Serialization
程序集: System.Xml(位于 System.Xml.dll)
注意:
反序列化是︰ 读取的 XML 文档,并构造对象强类型化到 XML 架构 (XSD) 文档的过程。
在反序列化之前, XmlSerializer 必须使用要反序列化的对象的类型构造。
下面举个例子说明:
比如说有一个序列化后的xml文件,内容如下:
<?xml version="1.0"?>
<OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<inventory:ItemName>Widget</inventory:ItemName>
<inventory:Description>Regular Widget</inventory:Description>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity></inventory:Quantity>
<money:LineTotal></money:LineTotal>
</OrderedItem>
我们可以通过以下方法,把这个文件反序列化成一个OrderedItem类型的对象,看下面的例子:
using System;
using System.IO;
using System.Xml.Serialization; // This is the class that will be deserialized.
public class OrderedItem
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace="http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace="http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
} public class Test
{
public static void Main()
{
Test t = new Test();
// Read a purchase order.
t.DeserializeObject("simple.xml");
} private void DeserializeObject(string filename)
{
Console.WriteLine("Reading with Stream");
// Create an instance of the XmlSerializer.
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem)); // Declare an object variable of the type to be deserialized.
OrderedItem i; using (Stream reader = new FileStream(filename, FileMode.Open))
{
// Call the Deserialize method to restore the object's state.
i = (OrderedItem)serializer.Deserialize(reader);
} // Write out the properties of the object.
Console.Write(
i.ItemName + "\t" +
i.Description + "\t" +
i.UnitPrice + "\t" +
i.Quantity + "\t" +
i.LineTotal);
}
}
C#语言中的XmlSerializer类的XmlSerializer.Deserialize (Stream)方法举例详解的更多相关文章
- C#语言中的XmlSerializer类的XmlSerializer.Serialize(Stream,Object)方法举例详解
在对象和 XML 文档之间进行序列化和反序列化操作. XmlSerializer 使您能够控制如何将对象编码为 XML. 命名空间: System.Xml.Serialization程序集: S ...
- JAVA类与类之间的全部关系简述+代码详解
本文转自: https://blog.csdn.net/wq6ylg08/article/details/81092056类和类之间关系包括了 is a,has a, use a三种关系(1)is a ...
- 第8.13节 Python类中内置方法__repr__详解
当我们在交互环境下输入对象时会直接显示对象的信息,交互环境下输入print(对象)或代码中print(对象)也会输出对象的信息,这些输出信息与两个内置方法:__str__方法和__repr__方法有关 ...
- python语言中threading.Thread类的使用方法
1. 编程语言里面的任务和线程是很重要的一个功能.在python里面,线程的创建有两种方式,其一使用Thread类创建 # 导入Python标准库中的Thread模块 from threading i ...
- python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解
1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...
- C++ string 类的 find 方法实例详解
1.C++ 中 string 类的 find 方法列表 size_type std::basic_string::find(const basic_string &__str, size_ty ...
- 类的特殊方法"__call__"详解
1. __call__ 当执行对象名+括号时, 会自动执行类中的"__call__"方法, 怎么用? class A: def __init__(self, name): self ...
- python 类中__init__,__new__,__class__的使用详解
1.python中所有类默认继承object类,而object类提供了很多原始的内置属性和方法,所有用户定义的类在python 中也会继承这些内置属性.我们可以通过dir()进行查看.虽然python ...
- 第8.14节 Python类中内置方法__str__详解
一. object类内置方法__str__和函数str 类的内置方法__str__和内置函数str实际上实现的是同一功能,实际上str调用的就是__str__方法,只是调用方式不同,二者的调用语法如下 ...
随机推荐
- Spring注入
Spring注入 Spring注入是指在启动Spring容器加载bean配置的时候,完成对变量的赋值行为. 常用的两种注入方式: setter注入 构造注入 <?xml version=&quo ...
- 经过一段的努力,终于成为CSDN博客专家,感谢大家支持
感谢CSDN提供这么好的一个技术学习平台,通过各路大神的博客我成长了许多,同时也感谢支持我的朋友们,我会继续努力,用心去写好博客.还请继续关注我~ 谢谢!
- Linux 学习笔记_12_Windows与Linux文件共享服务_1.1_--Samba(上)
Samba简介:在UNIX系统中,Samba是通过服务器消息块协议(SMB)在网络上的计算机之间,共享文件和打印服务的软件包. SMB简介:Server Message Block,SMB协议是一种服 ...
- 【算法导论】单源最短路径之Bellman-Ford算法
单源最短路径指的是从一个顶点到其它顶点的具有最小权值的路径.我们之前提到的广度优先搜索算法就是一种无权图上执行的最短路径算法,即在所有的边都具有单位权值的图的一种算法.单源最短路径算法可以解决图中任意 ...
- 【Matlab编程】Matlab及Java小时钟
一年前曾经用matlab的gui做了一个时钟,由于是直接用GUIDE和ActiveX控件写的,程序虽说有许多行,大多数都是自动生成的,自己写的只有十几行而已.闲着没事,就耗费了下午的时间用matlab ...
- Java-Filter-FilterChain-FilterConfig源码
public interface Filter { /** * Called by the web container to indicate to a filter that it is being ...
- 用C语言绘制一条标准的余弦曲线
#include<stdio.h> #include<math.h> int main() { double y; int x,m; for(y=1;y>=-1;y-=0 ...
- Linux 获得机器的IP和网卡信息
Linux 获得机器的IP和网卡信息 代码来自于网络, 我改写了, 有美不敢自专, 特分享之.用法很简单,就3个函数. 头文件getmac.h: /** * getmac.h * * 2014-07- ...
- 基于友善之臂ARM-ContexA9-ADC驱动开发
ADC,就是模数转换器,什么是模数转换器? 模数转换器,在电子技术中即是将模拟信号转换成数字信号,也称为数字量化. 当然还有一种叫DAC,就是数模转换,意思相反,即是将数字信号转换成模拟信号. 在友善 ...
- android 应用模式之mvp
说到MVP就不得不提到MVC,做过J2EE的猿友们肯定知道MVC是个什么东西.MVC即 Model.View.Controller, 那MVP就Model.View.Presenter.Model用于 ...