序列化对象

    public class People
{
[XmlAttribute("NAME")]
public string Name
{ set; get; }
[XmlAttribute("AGE")]
public int Age
{ set; get; }
}
[XmlRoot("Root")]
public class Student : People
{
[XmlElement("CLASS")]
public string Class
{ set; get; }
[XmlElement("NUMBER")]
public int Number
{ set; get; }
} void Main(string[] args) { Student stu = new Student()
{
Age = ,
Class = "Class One",
Name = "Tom",
Number =
};
XmlSerializer ser = new XmlSerializer(typeof(Student));
ser.Serialize(File.Create("C:\\x.xml"), stu); }

反序列化对象

XmlSerializer ser = new XmlSerializer(typeof(Student));
            Student stu = ser.Deserialize(File.OpenRead("C:\\x.xml")) as Student;

对象数组序列化

public class People
    {
        [XmlAttribute("NAME")]
        public string Name
        { set; get; }
        [XmlAttribute("AGE")]
        public int Age
        { set; get; }
    }
    [XmlRoot("Root")]
    public class Student : People
    {
        [XmlElement("CLASS")]
        public string Class
        { set; get; }
        [XmlElement("NUMBER")]
        public int Number
        { set; get; }
    }

void Main(string[] args)

{

List<Student> stuList = new List<Student>();
            stuList.Add(new Student() { Age = 10, Number = 1, Name = "Tom", Class = "Class One" });
            stuList.Add(new Student() { Age = 11, Number = 2, Name = "Jay", Class = "Class Two" });
            stuList.Add(new Student() { Age = 12, Number = 3, Name = "Pet", Class = "Class One" });
            stuList.Add(new Student() { Age = 13, Number = 4, Name = "May", Class = "Class Three" });
            stuList.Add(new Student() { Age = 14, Number = 5, Name = "Soy", Class = "Class Two" });
            XmlSerializer ser = new XmlSerializer(typeof(List<Student>));
            ser.Serialize(File.Create("C:\\x.xml"), stuList);

}

对象数组反序列

XmlSerializer ser = new XmlSerializer(typeof(List<Student>));
            List<Student> stuList = ser.Deserialize(File.OpenRead("C:\\x.xml")) as List<Student>;
            foreach (Student s in stuList)
            {
                MessageBox.Show(string.Format("{0} : {1} : {2} : {3}",
                    s.Name, s.Age, s.Class, s.Number));
            }

序列化Dirctionary

public struct DirectionList
    {
        [XmlAttribute("Name")]
        public string Name;
        [XmlElement("Value")]
        public int Value;
    }

void Main(string[] args)

{

Dictionary<string, int> list = new Dictionary<string, int>();
            list.Add("1", 100);
            list.Add("2", 200);
            list.Add("3", 300);
            list.Add("4", 400);
            list.Add("5", 500);
            list.Add("6", 600);
            list.Add("7", 700);
            list.Add("8", 800);
            list.Add("9", 900);

List<DirectionList> dirList = new List<DirectionList>();
            foreach (var s in list)
            {
                dirList.Add(new DirectionList() { Name = s.Key, Value = s.Value });
            }
            XmlSerializer ser = new XmlSerializer(typeof(List<DirectionList>));
            ser.Serialize(File.Create("C:\\x.xml"), dirList);

}

这里还要讲一点,在XmlSerializer中,不支持Dirctionary<>类型的对象,所以在序列化这种最常见类型的时候,只能按照它的格式先创建一个可以别序列化的类型,这里我定义了一个结构体,当然你也可以定义成其他的类。将Dictionary<>中的数据依次放进结构体以后就可以放入流中了。

[XmlAttribute("Name")]意思是将这个字段作为xml的属性,属性名跟在“”中

[XmlElement("Value")]意思是将这个字段做为xml的元素。

反序列化Dirctionary

XmlSerializer ser = new XmlSerializer(typeof(List<DirectionList>));
            List<DirectionList> dirList = ser.Deserialize(
                File.OpenRead("C:\\x.xml")) as List<DirectionList>;
            foreach (var v in dirList)
            {
                Console.WriteLine("{0} : {1}", v.Name, v.Value);
            }

其实我并不喜欢这个名称,感觉有点生化危机的feel,但是也就是这样了,没有太炫的地方,Deserialize反序列化。真希望.Net能集成Dirctionary<>对象,那我们这些懒人就方便了。

在需要序列化的队伍中,数组是很常见的类型,其次就是图片了

序列化图片

public struct ImageStruct
    {
        [XmlAttribute("Number")]
        public int number;
        [XmlElement("Image")]
        public byte[] picture;
    }

void Main(string[] args)

{

ImageStruct s = new ImageStruct() { number = 1, picture = File.ReadAllBytes(@"11.jpg") };
            XmlSerializer ser = new XmlSerializer(typeof(ImageStruct));
            FileStream fs = File.Create("c:\\x.xml");
            ser.Serialize(fs, s);
            fs.Close();

}

一样的,采用结构体来保存图片,这里我还加了个图片的名字,到时候查找起来也方便一些

图片反序列化

XmlSerializer ser = new XmlSerializer(typeof(ImageStruct));
            ImageStruct s = (ImageStruct)ser.Deserialize(File.OpenRead("c:\\x.xml"));
            pictureBox1.Image = Image.FromStream(new MemoryStream(s.picture));

没有花头的方式,利用memorystream来做缓存,这样会比较快一点,实际上我并没有怎么感觉。

图片数组序列化

public struct ImageStruct
    {
        [XmlAttribute("Number")]
        public int number;
        [XmlElement("Image")]
        public byte[] picture;
    }

void Main(string[] args)

{

List<ImageStruct> imageList = new List<ImageStruct>();
            imageList.Add(new ImageStruct()
            {
                number = 1,
                picture = File.ReadAllBytes(@"11.jpg")
            });
            imageList.Add(new ImageStruct()
            {
                number = 2,
                picture = File.ReadAllBytes(@"22.jpg")
            });

XmlSerializer ser = new XmlSerializer(typeof(List<ImageStruct>));
            FileStream fs = File.Create("c:\\x.xml");
            ser.Serialize(fs, imageList);
            fs.Close();

}

图片数组反序列化

XmlSerializer ser = new XmlSerializer(typeof(List<ImageStruct>));
            List<ImageStruct> s = (List<ImageStruct>)ser.Deserialize(File.OpenRead("c:\\x.xml"));
            var im = from i in s
                     where i.number == 1
                     select i.picture;

//var im = s.Where(p => p.number == 1).Select(p => p.picture);
            foreach (var image in im)
            {
                pictureBox1.Image = Image.FromStream(
                    new MemoryStream(image));
            }

这里还对数组结构进行了Linq查询,这样就可以很方便的查询图片了。

c# XML序列化与反序列化 属性字段标识的更多相关文章

  1. .NET中XML序列化和反序列化常用类和用来控制XML序列化的属性总结(XmlSerializer,XmlTypeAttribute,XmlElementAttribute,XmlAttributeAttribute,XmlArrayAttribute...)

    序列化和反序列化是指什么? 序列化(seriallization): 将对象转化为便于传输的数据格式, 常见的序列化格式:二进制格式,字节数组,json字符串,xml字符串.反序列化(deserial ...

  2. XML 序列化与反序列化

    XML序列化与反序列化 1.将一个类转化为XML文件 /// <summary> /// 对象序列化成XML文件 /// </summary> /// <param na ...

  3. c# XML序列化与反序列化

    c# XML序列化与反序列化 原先一直用BinaryFormatter来序列化挺好,可是最近发现在WinCE下是没有办法进行BinaryFormatter操作,很不爽,只能改成了BinaryWrite ...

  4. Windows phone 之XML序列化与反序列化

    为什么要做序列化和反序列化? 一个回答: 我们都知道对象是不能在网络中直接传输的,不过还有补救的办法.XML(Extensible Markup Language)可扩展标记语言,本身就被设计用来存储 ...

  5. C# Note4:XML序列化和反序列化(含加密解密等)

    前言 在项目中,我们经常用到各种配置文件,比如xml文件.binary文件等等,这里主要根据实践经验介绍下xml文件的序列化和反序列化(毕竟最常用). 实践背景:我要做一个用户管理功能,用户账号信息存 ...

  6. XmlSerializer 对象的Xml序列化和反序列化

    http://www.cnblogs.com/yukaizhao/archive/2011/07/22/xml-serialization.html 这篇随笔对应的.Net命名空间是System.Xm ...

  7. 第一章 JacksonUtil 序列化与反序列化属性总结

    1.json-lib与Jackson 关于json-lib与Jackson对比总结如下: 1).性能方面,Jackson的处理能力高出Json-lib10倍左右. 2).json-lib已经停止更新, ...

  8. XmlSerializer 对象的Xml序列化和反序列化,XMLROOT别名设置

    这篇随笔对应的.Net命名空间是System.Xml.Serialization:文中的示例代码需要引用这个命名空间.   为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中:内存中 ...

  9. C#操作Xml:XmlSerializer 对象的Xml序列化和反序列化

    这篇随笔对应的.Net命名空间是System.Xml.Serialization:文中的示例代码需要引用这个命名空间. 为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中:内存中的对 ...

随机推荐

  1. Mono addin 学习笔记 4 再论数据扩展点(Data only extension point)

    1. Attribute声明方式 定义扩展属性 [AttributeUsage(AttributeTargets.Assembly, AllowMultiple= true)] public clas ...

  2. Linux下编译带x264的ffmpeg的配置方法,包含SDL2

    一.环境准备 ffmpeg下载:http://www.ffmpeg.org/download.html x264下载:http://download.videolan.org/x264/snapsho ...

  3. 一个页面从输入URL 到页面加载显示完成的过程中都发生了什么

    前端面试/笔试必考问题,越详细越好 先简单得讲: 浏览器根据请求的URL交给DNS域名解析,找到真实IP,向服务器发起请求: 服务器交给后台处理完成后返回数据,浏览器接收文件(HTML.JS.CSS. ...

  4. openldap主机访问控制(基于ip)

    http://blog.oddbit.com/2013/07/22/generating-a-membero/ http://gsr-linux.blogspot.jp/2011/01/howto-o ...

  5. AX 4.0 调用打印设定的功能

    PrintJobSettings printJobSettings; PrintJobSettings printJobSettings2; Boolean ok; container packPri ...

  6. MySQL 常用命令(持续更新)

    停止启动MySQL服务 停止:net stop mysql启动:net start mysql 查看正在运行的线程 SHOW PROCESSLIST SHOW FULL PROCESSLIST 杀死线 ...

  7. Linux相关文章

    1.linux 中特殊符号用法详解 2.linux之vim命令 3.linux各文件夹的作用 4.修改linux文件权限命令:chmod 5.CentOS 6.6下安装配置Tomcat环境 6.lin ...

  8. linux C学习笔记05--信号量与共享内存(进程同步)

    花了半天时间把信号量与共享内存整合到了一起,先来看信号量代码,semaphore.c 当中sem_P()和sem_V()函数分别为信号量等待和信号量释放. 两个进程同时访问共享内存,为了避免发生同时读 ...

  9. 添加和删除hadoop集群中的节点

    参见 http://www.cnblogs.com/tommyli/p/3418273.html

  10. iOS定位到崩溃代码行数

    不知道大家是不是在代码调试过程中经常遇到项目崩溃的情况: 比如: 数组越界: 没有实现方法选择器: 野指针: 还有很多很多情况.......昨天学到了一种可以直接定位到崩溃代码行数的一个命令,记录一下 ...