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. ...
随机推荐
- CentOS卸载系统自带的OpenJDK
查看目前系统的jdk: rpm -qa | grep jdk 得到的结果: $ rpm -qa | grep jdk java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el ...
- 2017-5-14 湘潭市赛 Similar Subsequence 分析+四维dp+一些简单优化
Similar Subsequence Accepted : Submit : Time Limit : MS Memory Limit : KB Similar Subsequence For gi ...
- Sublime 中 SFTP插件的使用
首先说明的是Sumblime Text 3,下载安装后,打开软件, 按下Ctrl+Shift+P调出命令面板, 按回车键后弹出下面的 然后 点击左上角的 文件 >SFTP/FTP > Se ...
- 24模拟keepalved vrrp功能,监听主节点,如果主节点不可访问则备节点启动并配置LVS实现接管主节点的资源提供服务(提醒:注意ARP缓存)
[root@lb04 scripts]# cat ha_lv.sh #!/bin/bash while true do check_count=$(nmap 10.0.0.13|grep " ...
- php 循环简写
<?php class a{ public function news($news){ $articles = array(); foreach ($news as $key => $va ...
- 如何查询当前手机的cpu架构,so库导入工程又出异常了?
执行adb命令: adb shell cat /proc/cpuinfo 对应文件夹 AArch64 == arm64-v8a ARMv7 == armeabi-v7a ............等 其 ...
- JavaScript 与 Java 是两种完全不同的语言,无论在概念还是设计上。
JavaScript 与 Java 是两种完全不同的语言,无论在概念还是设计上. Java(由 Sun 发明)是更复杂的编程语言. ECMA-262 是 JavaScript 标准的官方名称. Jav ...
- Laravel创建模型
laravel中创建模型 <?php /** * Created by PhpStorm. * 新建模型 * User: chuang * Date: 17-1-15 * Time: 上午9:1 ...
- nginx配置后只有根目录首页index.php能访问,其他页面404
只有首页面根目录可以访问,其他页面地址都是404 not found.网上找了半天url重定向,url重写都试了无效,要不就是重定向过多,下图为跳坑历程. location / { #if ($htt ...
- getParameterMap的使用
就是前端提交到Servlet或者Action里面的参数Map哈,如果你是以表单提交,那么request.getParameterMap()中将包含你表单里面所有input标签的数据,以其name为ke ...