C# 二进制序列化(BinaryFormatter),Xml序列化(XmlSerializer),自己模拟写一个Xml序列化过程。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization; namespace 序列化
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Person p = new Person();
p.Name ="张三";
p.Age =;
p.Gender = true; using (System.IO.FileStream file = new FileStream("person.dat", FileMode.Create, FileAccess.Write))
{
//System.Runtime.Serialization.Formatters.Binary;
BinaryFormatter b = new BinaryFormatter();
b.Serialize(file, p); //序列化
}
MessageBox.Show("ok");
} private void button2_Click(object sender, EventArgs e)
{
using (System.IO.FileStream file = new FileStream("person.dat", FileMode.Open, FileAccess.Read))
{
BinaryFormatter b = new BinaryFormatter();
Person p = (Person)b.Deserialize(file); //反序列化 MessageBox.Show(p.Name);
}
} private void button3_Click(object sender, EventArgs e)
{
List<Person> list = new List<Person>();
Person p = new Person();
p.Name = "张三";
p.Age = ;
p.Gender = true;
Person p2 = new Person();
p2.Name = "李四";
p2.Age = ;
p2.Gender = true; list.Add(p);
list.Add(p2); //将Person类对象序列化写入流中
using (System.IO.FileStream file = File.OpenWrite("person.xml"))
{
//System.Xml.Serialization;
XmlSerializer xmlSer = new XmlSerializer(typeof(Person));
xmlSer.Serialize(file, p);
} //也可以将List集合对象序列化写入到流中,对象是Object类型。
using (System.IO.FileStream file = File.OpenWrite("personList.xml"))
{
XmlSerializer xmlSer = new XmlSerializer(typeof(List<Person>)); //通过反射指定类型
xmlSer.Serialize(file, list);
} MessageBox.Show("ok");
} private void button4_Click(object sender, EventArgs e)
{
//Xml反序列化
using (System.IO.FileStream file = File.OpenRead("person.xml"))
{
XmlSerializer xmlSer = new XmlSerializer(typeof(Person));
Person p = (Person)xmlSer.Deserialize(file);
MessageBox.Show(p.Name);
}
}
}
//程序集“序列化, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中的类型“序列化.Person”未标记为可序列化。
[Serializable] //Serializable 可以被序列化,此类不能被继承。 //XML序列化中不需要此方法!
public class Person
{
private string name;
private int age;
private bool gender; [XmlIgnore] //XmlIgnoreAttribute 不序列化公共字段或公共读写属性值。
public bool Gender
{
get { return gender; }
set { gender = value; }
} public int Age
{
get { return age; }
set { age = value; }
} public string Name
{
get { return name; }
set { name = value; }
} }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization; namespace 序列化
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
MyPerson mp = new MyPerson();
mp.Name = "张三";
mp.Age = ;
mp.Gender = true; using (System.IO.FileStream file = new System.IO.FileStream("myPerson.xml", System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
//创建自己写的序列化类对象
MyXmlSerializer myXmlSer = new MyXmlSerializer();
//调用序列化方法,保存到文件中。
myXmlSer.Serialize(file, mp);
}
MessageBox.Show("ok");
}
} public class MyPerson
{
private string name;
private int age;
private bool gender; [MyXmlIgnore] //添加特性,这个公有的属性将不被序列化,在序列化方法里实现。
public bool Gender
{
get { return gender; }
set { gender = value; }
} public int Age
{
get { return age; }
set { age = value; }
} public string Name
{
get { return name; }
set { name = value; }
} }
//模拟写一个序列化方法类
public class MyXmlSerializer
{
/// <summary>
/// 使用指定的 System.IO.Stream 序列化指定的 System.Object 并将 XML 文档写入文件。
/// </summary>
/// <param name="stream">用于编写 XML 文档的 System.IO.Stream。</param>
/// <param name="o">将要序列化的 System.Object。</param>
public void Serialize(System.IO.Stream stream, object o)
{
//通过反射获取对象类型
Type typeObj = o.GetType(); //创建XML文档
System.Xml.Linq.XDocument xdoc = new System.Xml.Linq.XDocument();
//创建XML元素,获取对象类型类名称作为,XML根节点名称
System.Xml.Linq.XElement xRoot = new System.Xml.Linq.XElement(typeObj.Name);
//添加到XML根节点
xdoc.Add(xRoot); //通过反射获取这个类型的所有公共属性
System.Reflection.PropertyInfo[] pinfos = typeObj.GetProperties(); //遍历pinfos每个属性都有创建XML元素
foreach (System.Reflection.PropertyInfo info in pinfos)
{
//通过每个属性获取它的特性,判断有没有MyXmlIgnoreAttribute特性,如果没有才给序列化。
object[] objAttrs = info.GetCustomAttributes(typeof(MyXmlIgnoreAttribute), false);
//当小于等于0时,可判定该属性没有MyXmlIgnoreAttribute特性,可以被序列化。
if (objAttrs.Length <= )
{
System.Xml.Linq.XElement xelet = new System.Xml.Linq.XElement(info.Name, info.GetValue(o));
xRoot.Add(xelet); //添加到根节点下面
}
//有MyXmlIgnoreAttribute特性的就不写入XML。
}
//保存XML到流中去
xdoc.Save(stream);
}
} //不序列化属性字段特性
public class MyXmlIgnoreAttribute : Attribute
{ }
}
关于反序列化:
<?xml version="1.0"?>
<PersonRoot>
<Person>
<Age>18</Age>
<Name>张三</Name>
</Person>
<Person>
<Age>19</Age>
<Name>李四</Name>
</Person>
</PersonRoot>
public class PersonRoot
{
List<Person> perList = new List<Person>(); [XmlElement(ElementName = "Person")]
public List<Person> PerList
{
get { return perList; }
set { perList = value; }
}
}
using (System.IO.FileStream file = File.OpenRead("personList.xml"))
{
XmlSerializer xmlSer = new XmlSerializer(typeof(PersonRoot));
PersonRoot list = (PersonRoot)xmlSer.Deserialize(file);
MessageBox.Show(list.PerList[].Name);
}
XML数据格式,
<ArrayOfPerson>
<Person>
可以直接List<Person>类型反序列化到List<Person>对象中。
Attribute:
Attribute 类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。 目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性、返回值、结构或其他特性。
特性所提供的信息也称为元数据。 元数据可由应用程序在运行时进行检查以控制程序处理数据的方式,也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式。 例如,.NET Framework 预定义特性类型并使用特性类型控制运行时行为,某些编程语言使用特性类型表示 .NET Framework 常规类型系统不直接支持的语言功能。
所有特性类型都直接或间接地从 Attribute 类派生。 特性可应用于任何目标元素;多个特性可应用于同一目标元素;并且特性可由从目标元素派生的元素继承。 使用 AttributeTargets 类可以指定特性所应用到的目标元素。
Attribute 类提供检索和测试自定义特性的简便方法。 有关使用特性的更多信息,请参见 应用特性和 利用特性扩展元数据。
下面的代码示例演示 Attribute 的用法。
using System;
using System.Reflection; // An enumeration of animals. Start at 1 (0 = uninitialized).
public enum Animal {
// Pets.
Dog = ,
Cat,
Bird,
} // A custom attribute to allow a target to have a pet.
public class AnimalTypeAttribute : Attribute {
// The constructor is called when the attribute is set.
public AnimalTypeAttribute(Animal pet) {
thePet = pet;
} // Keep a variable internally ...
protected Animal thePet; // .. and show a copy to the outside world.
public Animal Pet {
get { return thePet; }
set { thePet = value; }
}
} // A test class where each method has its own pet.
class AnimalTypeTestClass {
[AnimalType(Animal.Dog)]
public void DogMethod() {} [AnimalType(Animal.Cat)]
public void CatMethod() {} [AnimalType(Animal.Bird)]
public void BirdMethod() {}
} class DemoClass {
static void Main(string[] args) {
AnimalTypeTestClass testClass = new AnimalTypeTestClass();
Type type = testClass.GetType();
// Iterate through all the methods of the class.
foreach(MethodInfo mInfo in type.GetMethods()) {
// Iterate through all the Attributes for each method.
foreach (Attribute attr in
Attribute.GetCustomAttributes(mInfo)) {
// Check for the AnimalType attribute.
if (attr.GetType() == typeof(AnimalTypeAttribute))
Console.WriteLine(
"Method {0} has a pet {1} attribute.",
mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
} }
}
}
/*
* Output:
* Method DogMethod has a pet Dog attribute.
* Method CatMethod has a pet Cat attribute.
* Method BirdMethod has a pet Bird attribute.
*/
C# 二进制序列化(BinaryFormatter),Xml序列化(XmlSerializer),自己模拟写一个Xml序列化过程。的更多相关文章
- 写一个xml文件到磁盘的方法
/** * 往磁盘上写一个xml文件 * * <?xml version="1.0" encoding="UTF-8" standalone=" ...
- 一个可序列化的C#对象,如何转成一个XML格式的文件或字符串【转】
原文:http://blog.csdn.net/otong/article/details/7894059 序列化或反序列化成一个字符串: 方法一: 序列化: public static string ...
- 把xml格式的字符串写入到一个xml文件中
package demo; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; impo ...
- 通常一个 Xml 映射文件,都会写一个 Dao 接口与之对应, 请问,这个 Dao 接口的工作原理是什么?Dao 接口里的方法, 参数不同时,方法能重载吗?
Dao 接口即 Mapper 接口.接口的全限名,就是映射文件中的 namespace 的值: 接口的方法名,就是映射文件中 Mapper 的 Statement 的 id 值:接口方法内的 参数,就 ...
- 通常一个Xml映射文件,都会写一个Dao接口与之对应,请问,这个Dao接口的工作原理是什么?Dao接口里的方法,参数不同时,方法能重载吗?
Dao接口即Mapper接口.接口的全限名,就是映射文件中的namespace的值:接口的方法名,就是映射文件中Mapper的Statement的id值:接口方法内的参数,就是传递给sql的参数. M ...
- testng入门教程16数据驱动(把数据写在xml)
testng入门教程16数据驱动(把数据写在xml) testng入门教程16数据驱动(把数据写在xml)把数据写在xml文件里面,在xml文件右键选择runas---testng执行 下面是case ...
- c# 序列化BinaryFormatter、SoapFormatter和XmlSerializer的区别
在C#中常见的序列化的方法主要也有三个:BinaryFormatter.SoapFormatter.XML序列化 1.BinaryFormatter 序列化 [Serializable] //如果要想 ...
- C#操作Xml:XmlSerializer 对象的Xml序列化和反序列化
这篇随笔对应的.Net命名空间是System.Xml.Serialization:文中的示例代码需要引用这个命名空间. 为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中:内存中的对 ...
- wcf中序列化BinaryFormatter,DataContractJsonSerializer,DataContractSerializer,SoapFormatter,XmlSerializer
using System; using System.Runtime.Serialization; using System.Xml.Serialization; namespace Larryle. ...
随机推荐
- hdu1873 看病要排队 优先队列
看病要排队 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 带你了解UIKit动力学
一.简单介绍 1.什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象如:重力.弹性碰撞等现象 ...
- ListView中pointToPosition()方法使用具体演示样例
MainActivity例如以下: package cc.testpointtoposition; import java.util.ArrayList; import java.util.HashM ...
- 【转】Android自动化测试(UiAutomator)——UiObject
本文主要讲解使用UiAutomator的一些技巧,希望对于初学者有一定的帮助 UiObject 1.首先要声明对象 UiObject XXX = new UiObject(new Selector) ...
- java 读取world的图片 并把图片路径存入数据库
package World; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcep ...
- jetty端口灵活配置方法
在使用maven开发web项目极大地方便了jar包的依赖,在测试时也可以集成Servlet容器,从启动速度和量级上看,Jetty无疑是不二选择. 如果多个项目同时启动,就会端口冲突了. 一种办法是通过 ...
- JS异步笔记
Promise 最早接触异步是在.net中,当时还是比较流行使用基于控件的BackgroundWorker,其自身通过子线程的方式来异步处理一些情况,并且封装了一些功能与主线程通信.后来,开始使用Th ...
- jQuery + Cookie引导客户操作
网址:http://www.sucaihuo.com/js/707.html 示例:http://www.sucaihuo.com/jquery/7/707/demo/
- -webkit-transition: all .2s ease-in-out;
W3C标准中对CSS3的transition这是样描述的:CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发,并 ...
- linux 复制粘贴
如果想把文件内的第三行内容黏贴到第十五行: 1.进入你的文件,(处于命令模式,而不是编辑模式) 2.将你的光标移到你的第三行的开头(用方向键) 3,按下v,(你发现切换到可视模式了),按方向键,知道选 ...