在对象和 XML 文档之间进行序列化和反序列化操作。 XmlSerializer 使您能够控制如何将对象编码为 XML。

命名空间:   System.Xml.Serialization
程序集:  System.Xml(位于 System.Xml.dll)

下面举个例子说明:

// This is the class that will be serialized.
public class OrderedItem
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal; // A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}

如何把这个类转换成一个xml文件呢,这时候就需要 XmlSerializer类来处理了。

它的Serialize(Stream, Object)这个方法,就是用来把一个user类的对象转换成xml文档的。

我们来看一个例子:

using System;
using System.IO;
using System.Xml.Serialization;public class Test{
public static void Main(string[] args)
{
Test t = new Test();
// Write a purchase order.
t.SerializeObject("simple.xml");
} private void SerializeObject(string filename)
{
Console.WriteLine("Writing With Stream"); XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
OrderedItem i = new OrderedItem();
i.ItemName = "Widget";
i.Description = "Regular Widget";
i.Quantity = ;
i.UnitPrice = (decimal) 2.30;
i.Calculate(); // Create a FileStream to write with.
Stream writer = new FileStream(filename, FileMode.Create);
// Serialize the object, and close the TextWriter
serializer.Serialize(writer, i);
writer.Close();
}
}

所生成的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>

C#语言中的XmlSerializer类的XmlSerializer.Serialize(Stream,Object)方法举例详解的更多相关文章

  1. C#语言中的XmlSerializer类的XmlSerializer.Deserialize (Stream)方法举例详解

    包含由指定的 XML 文档反序列化 Stream. 命名空间:   System.Xml.Serialization程序集:  System.Xml(位于 System.Xml.dll) 注意: 反序 ...

  2. 007-Scala类的属性和对象私有字段实战详解

    007-Scala类的属性和对象私有字段实战详解 Scala类的使用实战 变量里的类必须赋初值 def函数时如果没参数可不带括号 2.不需要加Public声明 getter与setter实战 gett ...

  3. InheritableThreadLocal类原理简介使用 父子线程传递数据详解 多线程中篇(十八)

      上一篇文章中对ThreadLocal进行了详尽的介绍,另外还有一个类: InheritableThreadLocal 他是ThreadLocal的子类,那么这个类又有什么作用呢?   测试代码 p ...

  4. Java基础进阶:时间类要点摘要,时间Date类实现格式化与解析源码实现详解,LocalDateTime时间类格式化与解析源码实现详解,Period,Duration获取时间间隔与源码实现,程序异常解析与处理方式

    要点摘要 课堂笔记 日期相关 JDK7 日期类-Date 概述 表示一个时间点对象,这个时间点是以1970年1月1日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...

  5. java基础:详解类和对象,类和对象的应用,封装思想,构造方法详解,附练习案列

    1. 类和对象 面向对象和面向过程的思想对比 : 面向过程 :是一种以过程为中心的编程思想,实现功能的每一步,都是自己实现的 面向对象 :是一种以对象为中心的编程思想,通过指挥对象实现具体的功能 1. ...

  6. python语言中threading.Thread类的使用方法

    1. 编程语言里面的任务和线程是很重要的一个功能.在python里面,线程的创建有两种方式,其一使用Thread类创建 # 导入Python标准库中的Thread模块 from threading i ...

  7. C语言中scanf/fscanf 的%[]和%n说明符的使用方法

    标准输入输出函数%[]和%n说明符的使用方法     scanf fscanf,均从第一个非空格的可显示字符开始读起!         标准输入输出函数scanf具有相对较多的转换说明符,它常常作为入 ...

  8. hibernate(八) Hibernate检索策略(类级别,关联级别,批量检索)详解

    序言 很多看起来很难的东西其实并不难,关键是看自己是否花费了时间和精力去看,如果一个东西你能看得懂,同样的,别人也能看得懂,体现不出和别人的差距,所以当你觉得自己看了很多书或者学了很多东西的时候,你要 ...

  9. C++ string 类的 find 方法实例详解

    1.C++ 中 string 类的 find 方法列表 size_type std::basic_string::find(const basic_string &__str, size_ty ...

随机推荐

  1. 《重构》中Tips总结

    1         如果你发现自己需要为程序添加一个特性,而代码结构使你无法很方便地达到目的,那就先重构那个程序,使特性的添加比较容易进行,然后再添加特性. 2         重构之前,首先检查自己 ...

  2. Gradle脚本打包so库

    要让引用的第三方的so库被打包进去,只需要把相关的armeabi文件夹放在libs下面,然后在builld.gradle脚本中加上这一句: sourceSets{ main { jniLibs.src ...

  3. OC第二天—封装

    /.锁定头文件的方法 1. 打开终端 2. 进入到Xcode的目录, 命令:   cd /Applications/Xcode.app 3. 把系统头文件修改为只读, 命令:   sudo chown ...

  4. android ndk编译项目(android-ndk-16r1)

    由于采用android-ndk-16r1版本的ndk来编译 编译的环境之类在这里省略,注意是最后编译的命令如下 Administrator@WIN-AF6P80LVIJ0 ~ $ cd $ANDROI ...

  5. 网站论坛同步用户,整合api,实现…

    在网上参考了很多资料后,终于完美实现了网站和discuz!nt论坛的双向整合,整合后网站和论坛之间可以同步注册.登录.退出和修改登录密码操作. 本系统的实现形式是新云CMS网站(ASP)和Discuz ...

  6. 好看的dialog,sweet Alert Dialog 导入Android Studio

    系统自带的dialog实在是丑到无法忍受.所以找到了一款比较好的第三方dialog. github 地址如下:https://github.com/pedant/sweet-alert-dialog ...

  7. 【65】Mybatis详解

    Mybatis介绍 MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis能够 ...

  8. Java-ServletInputStream

    import java.io.InputStream; import java.io.IOException; /** * Provides an input stream for reading b ...

  9. myBatis源码之Executor、BaseExecutor和CachingExecutor

    接下来是mybatis的执行过程,mybatis提供了一个接口Executor,Executor接口主要提供了update.query方法及事物相关的方法接口 /** * @author Clinto ...

  10. SharePoint 2013 新建网站集图解(绝对菜鸟篇)

    前言:接触SharePoint的人可能是越来越多,但是很多人一接触就很迷茫,在技术群里问如何新建网站集,这样一篇图解,帮助新手学习在搭建好SharePoint环境之后,如何创建一个网站集,做一个基本的 ...