• XML反序列化一例


    <?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;
    
    
  • 相关阅读:
    DataTable 中各种计算(笔记)
    超级强悍开源图表控件ZedGraph,请高手指教数据绑定问题!!!
    ArcGIS Engine 9.3项目迁移到ArcGIS Engine 10 报错的解决方案
    c# ,vb.net代码相互转换
    ArcGIS扩展开发(一)为 ArcGIS Desktop建立Addin插件
    设计
    正则表达式
    JSON入门
    关于Unity中资源导入
    关于Unity中的ShaderLab
  • 原文地址:https://www.cnblogs.com/nanfei/p/2684496.html
Copyright © 2020-2023  润新知