关于自定义配置节,在此不多说,对于自己读取节点,然后自己静态缓存的(以前写过此类文章),也不多说
只说一下在使用反序列化时,节点标记与类名的注意问题(只在类上加序列化标记,各属性为默认序列)
问题出现:无法完成反序列化,不报错误,但配置没有读出
原因:由于个人代码习惯,类的第一个字母总是大写,而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>
///<configuration>
/// <configSections>
/// <section name="templateConfiguration"
/// type="JournalUpdateService.Config.TemplateConfigurationHandle, WindowsApplication1" />
/// </configSections>
///
/// <templateConfiguration>
/// <templates>
/// <template pageName="">
/// <head fileRef="" />
/// <body fileRef="" />
/// <bodyHeader fileRef="" />
/// <bodyContext fileRef="" />
/// <bodyFooter fileRef="" />
/// </template>
/// </templates>
/// </templateConfiguration>
/// </configuration>
[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>
/// <template pageName="">
/// <head fileRef="" />
/// <body fileRef="" />
/// <bodyHeader fileRef="" />
/// <bodyContext fileRef="" />
/// <bodyFooter fileRef="" />
/// </template>
[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;
}
}
}
}