<?xml version="1.0" encoding="utf-8" ?> <htmlPages> <htmlPage name="home"> <cssList> <css name ="base.css"></css> <css name ="font.css"></css> </cssList> <javascripts> <javascript name="jquery"></javascript> <javascript name="jquery.ui"></javascript> </javascripts> </htmlPage> <htmlPage name="about"> <cssList> <css name ="base2.css"></css> <css name ="font2.css"></css> </cssList> <javascripts> <javascript name="jquery2"></javascript> <javascript name="jquery2.ui"></javascript> </javascripts> </htmlPage> </htmlPages>
using System.Xml.Serialization; using System.Xml.Schema; [XmlRootAttribute(Namespace = "" , IsNullable = false)] public class htmlPages { [XmlElementAttribute("htmlPage", Form = XmlSchemaForm.Unqualified)] public HtmlPage[] Items { get; set; } } [XmlTypeAttribute(AnonymousType = true)] public class HtmlPage { [XmlArrayItemAttribute("css", typeof(Css), Form = XmlSchemaForm.Unqualified, IsNullable = false)] public Css[] cssList { get; set; } [XmlArrayItemAttribute("javascript", typeof(Javascript), Form = XmlSchemaForm.Unqualified, IsNullable = false)] public Javascript[] javascripts { get; set; } [XmlAttributeAttribute()] public string name { get; set; } } [XmlTypeAttribute(AnonymousType = true)] public partial class Css { [XmlAttributeAttribute()] public string name { get; set; } } [XmlTypeAttribute(AnonymousType = true)] public partial class Javascript { [XmlAttributeAttribute()] public string name { get; set; } }
var fs = new FileStream(string.Format(@"{0}\test.xml", Environment.CurrentDirectory), FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(htmlPages)); var htmlPages = serializer.Deserialize(fs) as htmlPages;