using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml.Serialization; using System.Xml; namespace DuFullCalendar { [XmlRoot("ArrayOfString")] public class ArrayOfString { [XmlElement("CityName")] public string CityName {get;set;} [XmlElement("CityCode")] public string CityCode { get; set; } } /// <summary> /// /// </summary> [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; } } /// <summary> /// /// </summary> public class General { [XmlAttribute] public string title { get; set; } [XmlAttribute] public string msg { get; set; } } /// <summary> /// /// </summary> public class Descrption { [XmlAttribute] public string document { get; set; } } /// <summary> /// /// </summary> 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<GeovinDu> GeovinDus { get; set; } [XmlElement(ElementName = "Sco")] public Sco sco { get; set; } [XmlElement(ElementName = "quest")] public Quest quest { get; set; } } /// <summary> /// /// </summary> public class GeovinDu { [XmlElement("id")] public int Id { get; set; } [XmlElement("KeyWord")] public string KeyWord { get; set; } } /// <summary> /// /// </summary> public class Sco : GeovinDu { [XmlAttribute] public string ScoTitle { get; set; } [XmlAttribute] public string isok { get; set; } } /// <summary> /// /// </summary> public class Quest : GeovinDu { [XmlAttribute] public string Title { get; set; } } }
调用:
//ArrayOfString arstring = new ArrayOfString() { // CityCode="0001", CityName="北京" //}; //string file = Server.MapPath("geovindu.xml"); //XmlSerializer xswrite = new XmlSerializer(typeof(ArrayOfString)); //写文件 //Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read); //xswrite.Serialize(stream, arstring); //读文件 //Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read); //ArrayOfString p = xswrite.Deserialize(stream) as ArrayOfString; //Response.Write(p.CityName); Teacher teacher = new Teacher(); teacher.LessonTitle = "geovindu 涂聚文"; General general = new General(); general.title = "ms"; general.msg = "hi,how are you"; teacher.general = general; Descrption descrption = new Descrption(); descrption.document = "你好,吗"; teacher.descrption = descrption; List<Part> partlist = new List<Part>(); Part part = new Part(); part.PartNum = "1"; part.PartOver = "2"; part.PartTitle = "p"; Sco sco = new Sco(); sco.ScoTitle = "sco"; sco.isok = "ok"; part.sco= sco; Quest quest = new Quest(); quest.Title = "quest"; part.quest = quest; List<GeovinDu> geovindu = new List<GeovinDu>(); GeovinDu du = new GeovinDu(); du.Id = 1; du.KeyWord = "geovindu"; geovindu.Add(du); du = new GeovinDu(); du.Id = 2; du.KeyWord = "geovindu2"; geovindu.Add(du); part.GeovinDus = geovindu; partlist.Add(part); part = new Part(); part.PartNum = "11"; part.PartOver = "22"; part.PartTitle = "pp"; sco = new Sco(); sco.ScoTitle = "sco"; sco.isok = "ok"; part.sco = sco; quest = new Quest(); quest.Title = "quest"; part.quest = quest; geovindu = new List<GeovinDu>(); du = new GeovinDu(); du.Id = 2; du.KeyWord = "geovindu"; geovindu.Add(du); du = new GeovinDu(); du.Id = 2; du.KeyWord = "geovindu2"; geovindu.Add(du); part.GeovinDus = geovindu; partlist.Add(part); teacher.partlist = partlist; string file = Server.MapPath("geovinduteach.xml"); XmlSerializer xswrite = new XmlSerializer(typeof(Teacher)); //Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read); //xswrite.Serialize(stream, teacher); Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read); Teacher p = xswrite.Deserialize(stream) as Teacher; Response.Write(p.LessonTitle);
xml file
<?xml version="1.0"?> <Teacher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" LessonTitle="geovindu 涂聚文"> <general title="ms" msg="hi,how are you" /> <descrption document="你好,吗" /> <Part PartTitle="p" PartNum="1" PartOver="2"> <GeovinDus> <GeovinDu> <id>1</id> <KeyWord>geovindu</KeyWord> </GeovinDu> <GeovinDu> <id>2</id> <KeyWord>geovindu2</KeyWord> </GeovinDu> </GeovinDus> <Sco ScoTitle="sco" isok="ok"> <id>0</id> </Sco> <quest Title="quest"> <id>0</id> </quest> </Part> <Part PartTitle="pp" PartNum="11" PartOver="22"> <GeovinDus> <GeovinDu> <id>2</id> <KeyWord>geovindu</KeyWord> </GeovinDu> <GeovinDu> <id>2</id> <KeyWord>geovindu2</KeyWord> </GeovinDu> </GeovinDus> <Sco ScoTitle="sco" isok="ok"> <id>0</id> </Sco> <quest Title="quest"> <id>0</id> </quest> </Part> </Teacher>
标签:XML,set,Deserialize,get,Serialize,part,new,du,public From: https://www.cnblogs.com/geovindu/p/17110925.html