zoukankan      html  css  js  c++  java
  • 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;
    
    
  • 相关阅读:
    C#综合揭秘——细说事务
    软件项目管理流程总结
    WCF揭秘——自定义绑定
    WCF揭秘——共享数据契约
    反流程升职记
    .NET基础篇——利用泛型与反射更新实体(ADO.NET Entity Framework)
    先睹为快:Visual Studio 11测试版已于2.29在微软官方网站正式发布
    .NET基础篇——分部类和分部方法
    C#综合揭秘——深入分析委托与事件
    .NET基础篇——反射的奥妙
  • 原文地址:https://www.cnblogs.com/nanfei/p/2684496.html
Copyright © 2011-2022 走看看