c# XML序列化與反序列化

序列化對象數組

    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)orm

{xml

            Student stu = new Student()
            {
                Age = 10,
                Class = "Class One",
                Name = "Tom",
                Number = 1
            };
            XmlSerializer ser = new XmlSerializer(typeof(Student));
            ser.Serialize(File.Create("C:\\x.xml"), stu);對象

}圖片

反序列化對象get

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

對象數組序列化it

    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; }
    }io

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查詢,這樣就能夠很方便的查詢圖片了。

相關文章
相關標籤/搜索