XmlSerializer. Deserialize解析xml

xml结构:

<?xml version="1.0" encoding="UTF-8" ?>
 <teacher LessonTitle="测试">
	<general title="你懂的" msg="不懂"/>        
	<descrption document="jjkk" />
	<part PartTitle="测试" PartNum="5" PartOver="1">
	    <sco ScoTitle="S001" isok="测试"  />
	    <quest Title="测试" />
	</part>
	<part PartTitle="测试" PartNum="5" PartOver="1">
	   	<sco ScoTitle="S001" isok="测试"  />
	    <quest Title="测试" />
		<!--间隔线-->
	    <sco ScoTitle="S001" isok="测试"  />
	    <quest Title="测试" />
	</part>
	<part PartTitle="测试">
	    <sco ScoTitle="S001" isok="测试"  />
	    <quest Title="测试" />
	    <!--间隔线-->
	    <sco ScoTitle="S001" isok="测试"  />
	    <quest Title="测试" />
	    <!--间隔线-->
		<sco ScoTitle="S001" isok="测试"  />
		<quest Title="测试" />
	</part>
	<!--....part部分-->
</teacher>
类结构:

    [XmlRoot]
    public class teacher
    {
        [XmlAttribute]
        public string LessonTitle { get; set; }

        [XmlElement]
        public general general { get; set; }

        [XmlElement]
        public descrption descrption { get; set; }

        [XmlElement("part")]
        public List<part> partlist { get; set; }
    }

    public class general
    {
        [XmlAttribute]
        public string title { get; set; }

        [XmlAttribute]
        public string msg { get; set; }
    }
    public class descrption
    {
        [XmlAttribute]
        public string document { get; set; }
    }

    //================>part
    public class part
    {
        [XmlAttribute]
        public string PartTitle { get; set; }

        [XmlAttribute]
        public string PartNum { get; set; }

        [XmlAttribute]
        public string PartOver { get; set; }

        [XmlElement(ElementName = "sco", Type = typeof(sco), IsNullable = true)]
        [XmlElement(ElementName = "quest", Type = typeof(quest), IsNullable = true)]
        public List<kk> kks { get; set; } 
        
    }

    public class kk
    {
    }
    public class sco : kk
    {
        [XmlAttribute]
        public string ScoTitle { get; set; }

        [XmlAttribute]
        public string isok { get; set; }
    }
    public class quest :kk
    {
        [XmlAttribute]
        public string Title { get; set; }
    }
获取teacher对象:

            XmlSerializer serializer = new XmlSerializer(typeof(teacher));


            using (TextReader reader = new StreamReader("XmlFile1.xml"))
            {
                teacher result = serializer.Deserialize(reader) as teacher;
            }




你可能感兴趣的:(deserialize,XMLSerializer,XmlRoot,XmlAttribute)