.Net中Config檔案中自訂配置節

來源:互聯網
上載者:User

關於自訂配置節,在此不多說,對於自己讀取節點,然後自己靜態緩衝的(以前寫過此類文章),也不多說

只說一下在使用還原序列化時,節點標記與類名的注意問題(只在類上加序列化標記,各屬性為預設序列)

問題出現:無法完成還原序列化,不報錯誤,但配置沒有讀出

原因:由於個人代碼習慣,類的第一個字母總是大寫,而XML標記第一個字母小寫

註:在使用還原序列化時,類名與標記名必須一致(區分大小寫),就我習慣來說,就是有一方必須不規範

當然,如果你每個屬性都加上序列化標記,指定XML元素名稱,則不在此列

程式碼範例:

1、設定檔範例,這是配置靜態頁模板的

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
  <section name="templateConfiguration" type="JournalUpdateService.Config.TemplateConfigurationHandle, WindowsApplication1" />
 </configSections>
 <templateConfiguration>
  <Templates>
   <Template pageName="Item.aspx">
    <head>E:/Projects/CNKIECP/Center/Web/WindowsApplication1/bin/Debug/Template/ITEM/Header.txt</head>
    <body>E:/Projects/CNKIECP/Center/Web/WindowsApplication1/bin/Debug/Template/ITEM/Body.txt</body>
    <bodyHeader>E:/Projects/CNKIECP/Center/Web/WindowsApplication1/bin/Debug/Template/ITEM/BodyHeader.txt</bodyHeader>
    <bodyContext>E:/Projects/CNKIECP/Center/Web/WindowsApplication1/bin/Debug/Template/ITEM/ItemBody.txt</bodyContext>
    <bodyFooter>E:/Projects/CNKIECP/Center/Web/WindowsApplication1/bin/Debug/Template/ITEM/BodyFooter.txt</bodyFooter>
   </Template>
  </Templates>
 </templateConfiguration>
 <appSettings>

 </appSettings>
</configuration> 

2、Handle 配置節點處理函數,實現IConfigurationSectionHandler 介面

using System;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;

namespace JournalUpdateService.Config
{
 /// <summary>
 /// TemplateConfigurationHandle 的摘要說明。
 /// </summary>
 public class TemplateConfigurationHandle : IConfigurationSectionHandler
 {
  public TemplateConfigurationHandle()
  {
  }

  public object Create(object parent, object configContext, System.Xml.XmlNode section)
  {
   XmlSerializer ser = new XmlSerializer(typeof(TemplateConfiguration));

   // Return the Deserialized object from the Web.config XML
   return ser.Deserialize(new XmlNodeReader(section));
  }
 }
}

3、Configration 自訂配置的根節點

using System;
using System.Collections;
using System.Xml.Serialization;

namespace JournalUpdateService.Config
{
 /// <summary>
 /// TemplateConfiguration 的摘要說明。
 /// </summary>
 ///&lt;configuration&gt;
 ///  &lt;configSections&gt;
 ///   &lt;section name="templateConfiguration"
 ///               type="JournalUpdateService.Config.TemplateConfigurationHandle, WindowsApplication1" /&gt;
 ///  &lt;/configSections&gt;
 ///  
 ///  &lt;templateConfiguration&gt;
 ///   &lt;templates&gt;
 ///    &lt;template pageName=""&gt;
 ///     &lt;head fileRef="" /&gt;
 ///     &lt;body fileRef="" /&gt;
 ///     &lt;bodyHeader fileRef="" /&gt;
 ///     &lt;bodyContext fileRef="" /&gt;
 ///     &lt;bodyFooter fileRef="" /&gt;
 ///    &lt;/template&gt;
 ///   &lt;/templates&gt;
 ///  &lt;/templateConfiguration&gt;
 /// &lt;/configuration&gt;
 [Serializable()]
 [XmlRoot("templateConfiguration")]
 public class TemplateConfiguration
 {
  private TemplateCollection _templates;
  public TemplateConfiguration()
  {
   //
   // TODO: 在此處添加建構函式邏輯
   //
  }

  public TemplateCollection Templates
  {
   get
   {
    return _templates;
   }
   set
   {
    _templates = value;
   }
  }
 }
}

4、Collection配置集合,實現CollectionBase介面

using System;
using System.Collections;

namespace JournalUpdateService.Config
{
 /// <summary>
 /// TemplateColection 的摘要說明。
 /// </summary>
 /// &lt;template pageName=""&gt;
 ///  &lt;head fileRef="" /&gt;
 ///  &lt;body fileRef="" /&gt;
 ///  &lt;bodyHeader fileRef="" /&gt;
 ///  &lt;bodyContext fileRef="" /&gt;
 ///  &lt;bodyFooter fileRef="" /&gt;
 /// &lt;/template&gt;
 [Serializable()]
 public class TemplateCollection : CollectionBase
 {
  public TemplateCollection()
  {
   //
   // TODO: 在此處添加建構函式邏輯
   //
  }
  public virtual void Add(Template r)
  {
   this.InnerList.Add(r);
  }

  public Template this[int index]
  {
   get
   {
    return (Template) this.InnerList[index];
   }
   set
   {
    this.InnerList[index] = value;
   }
  }
 }
}

5、配置實體

using System;
using System.Xml.Serialization;

namespace JournalUpdateService.Config
{
 /// <summary>
 /// Template 的摘要說明。
 /// </summary>
 [Serializable()]
 public class Template
 {
  private string _pageName;
  private string _head;
  private string _body;
  private string _bodyHeader;
  private string _bodyContext;
  private string _bodyFooter;

  public Template()
  {
  }

  [XmlAttribute]          //序列化為屬性
  public string pageName
  {
   get
   {
    return _pageName;
   }
   set
   {
    _pageName = value;
   }
  }

  public string head
  {
   get
   {
    return _head;
   }
   set
   {
    _head = value;
   }
  }

  public string body
  {
   get
   {
    return _body;
   }
   set
   {
    _body = value;
   }
  }

  public string bodyHeader
  {
   get
   {
    return _bodyHeader;
   }
   set
   {
    _bodyHeader = value;
   }
  }

  public string bodyContext
  {
   get
   {
    return _bodyContext;
   }
   set
   {
    _bodyContext = value;
   }
  }

  public string bodyFooter
  {
   get
   {
    return _bodyFooter;
   }
   set
   {
    _bodyFooter = value;
   }
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.