XmlSerializer. Deserialize解析xml

前端之家收集整理的这篇文章主要介绍了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;
            }
原文链接:https://www.f2er.com/xml/299040.html

猜你在找的XML相关文章