Xml Document与 xml反序列化

概念

.net 支持Xml文档与.net 类之间无缝双向转换。当应用程序规模变大后,要想程序变得灵活,可配置元素也会变得越来越多。应用程序根据xml文档配置也就自然而然的事。本文的目的也就讲解xml文档与.net 对象通过xml标签的对应关系,以及xml序列化出现问题后如何解决。

基本的序列化元素

[XmlRoot(“ElementName”)]:对应xml文档的根元素.

Xml Segment1

<?xml version="1.0" encoding="utf-8" ?>



<StudentCollection>



</StudentCollection>

 

对应的映射类:

复制代码
[XmlRoot("StudentCollection")]



    public class Student



    {



 



}
复制代码

 

 

Xml Segement 2

<?xml version="1.0" encoding="utf-8" ?>



<customer>



</customer>

映射类

复制代码
[XmlRoot("customer")]



    public class Customer



    {



 



    }
复制代码

 

 

XmlRoot(“元素名称”):类上的XmlRoot标签指明了类映射到xml的根结点,标签中的名称就是xml文档中根节点的名称。

[XmlElement(“ElementName”)]:映射到xml文档中元素。

Xml Segment1

复制代码
<?xml version="1.0" encoding="utf-8" ?>



<student>



  <age>30</age>



</student>
复制代码

 

 

对应的映射类:

复制代码
[XmlRoot("student")]



    public class Student



    {



        [XmlElement("age",Type=typeof (Int32))]



        public int Age



        {



            get;



            set;



 



        }



    }
复制代码


 

<?xml version="1.0" encoding="utf-8" ?>



<employee  age="30">



</employee>

 

[XmlAttribute(“AttributeName”)]:映射到xml文档中属性。

Xml 文件Employee.xml

 

对应的映射类:

复制代码
[XmlRoot("employee")]



    public class Employee



    {



        [XmlAttribute("age")]



        public string Age



        {



            get;



            set;



        }



    }
复制代码

 

Xml 解析:

复制代码
using (StreamReader sr = new StreamReader("Employee.xml"))



            {



                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Employee));



                Employee stList = xmlSerializer.Deserialize(sr) as Employee;



                Debug.Assert(stList == null);



                Console.Read();



            }
复制代码

 

同时具有XmlElement 与 XmlAttribute

复制代码
<?xml version="1.0" encoding="utf-8" ?>



<student id="1001">



  <age>10</age>



</student>
复制代码
复制代码
[XmlRoot("student")]



    public class Student



    {



        [XmlAttribute("id")]



        public string ID



        {



            get;



            set;



        }



        [XmlElement("age", Type = typeof(Int32))]



        public int Age



        {



            get;



            set;



        }



    }



 



 



using (StreamReader sr = new StreamReader("Student.xml"))



            {



                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Student));



                Student student = xmlSerializer.Deserialize(sr) as Student;



            }
复制代码

 

 

 

XmlAttayItem 与 XmlElement 以及XmlArray节点

复制代码
<?xml version="1.0" encoding="utf-8" ?>



<studentList>



  <student>



    <id>1001</id>



    <age>23</age>



    <name>hbb0b01</name>



    <subjects>



      <subject name="math">



        <score>



          80



        </score>



      </subject>



      <subject name="english">



        <score>



          90



        </score>



      </subject>



    </subjects>



  </student>



  <student>



    <id>1002</id>



    <age>30</age>



    <name>hbb0b02</name>



    <subjects>



      <subject name="math">



        <score>



          70



        </score>



      </subject>



      <subject name="english">



        <score>



          60



        </score>



      </subject>



    </subjects>



  </student>



</studentList>
复制代码

 

复制代码
public class Subject



    {



        [XmlAttribute("name")]



        public string name



        {



            get;



            set;



        }



 



        [XmlElement("score")]



        public int score



        {



            get;



            set;



        }



    }



 



    public class Student



    {



        public string id



        {



            get;



            set;



        }



        public string name



        {



            get;



            set;



        }



        public int age



        {



            get;



            set;



        }



         /// <summary>



        /// subjects可以与xml节点的集合节点的名称不同,可以用



        /// XmlArray指定映射到的集合节点



        /// </summary>



        [XmlArrayItem(ElementName = "subject", Type = typeof(Subject))]



        [XmlArray("subjects")]



        public Subject[] subjects123



        {



            get;



            set;



        }



 



    }



 



[XmlRoot("studentList")]



    public class StudentList



    {



        [XmlElement(ElementName = "student")]



        public List<Student> students



        {



            get;



            set;



        }



    }



 



using (StreamReader sr = new StreamReader("StudentList.xml"))



            {



                XmlSerializer xmlSerializer = new XmlSerializer(typeof(StudentList));



                StudentList stList = xmlSerializer.Deserialize(sr) as StudentList;



                Debug.Assert(stList == null);



                Console.Read();



            }
复制代码

 

总结:

至此最常用的xml标签XmlRoot,XmlAttribute,XmlElement,XmlArray,XmlArrayItem与类元素的映射关键已经讲完。

你可能感兴趣的:(document)