天天看點

.Net中Config檔案中自定義配置節

關于自定義配置節,在此不多說,對于自己讀取節點,然後自己靜态緩存的(以前寫過此類文章),也不多說

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

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

原因:由于個人代碼習慣,類的第一個字母總是大寫,而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;

   }

  }

 }

}