分享
 
 
 

.NET中自定义配置节点实例详解

王朝c#·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

一.目的

一般应用都有自己的配置文件,如何将配置文件映射到.NET中的对象是有现实意义的事情,在Java中有一个digester开源项目实现了这个功能,下面我一步一步来说明.NET中如何更简单的实现他

二.实现

1.定义xsd架构文件,我们定义几个简单的架构

源文件如下:

<?xml version="1.0" encoding="utf-8"?

<xs:schema id="MyConfig" targetNamespace="ConfigTest" elementFormDefault="qualified" xmlns="ConfigTest" xmlns:mstns="ConfigTest" xmlns:xs="http://www.w3.org/2001/XMLSchema"

<xs:complexType name="select" mixed="true"

<xs:sequence/

<xs:attribute name="id" type="xs:string" /

<xs:attribute name="resultMap" type="xs:string" /

<xs:attribute name="cacheModel" type="xs:string" use="optional" /

<xs:attribute name='sql' type='xs:string' /

</xs:complexType

<xs:complexType name="update" mixed="true"

<xs:sequence /

<xs:attribute name="id" type="xs:string" /

<xs:attribute name="parameterMap" type="xs:string" /

<xs:attribute name='sql' type='xs:string' /

</xs:complexType

<xs:element name="statements"

<xs:complexType

<xs:sequence

<xs:element name="select" type="select" minOccurs="0" maxOccurs="unbounded" /

<xs:element name="update" type="update" minOccurs="0" maxOccurs="unbounded" /

</xs:sequence

</xs:complexType

</xs:element

</xs:schema

2.使用xsd.exe生成对应于架构的配置类

xsd.exe MyConfig.xsd /c

//------------------------------------------------------------------------------

// <autogenerated

//

This code was generated by a tool.

//

Runtime Version:2.0.40607.16

//

//

Changes to this file may cause incorrect behavior and will be lost if

//

the code is regenerated.

// </autogenerated

//------------------------------------------------------------------------------

using System.Xml.Serialization;

//

// This source code was auto-generated by xsd, Version=2.0.40607.16.

//

namespace ConfigDll

{

/// <remarks/

[System.SerializableAttribute()]

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "ConfigTest")]

[System.Xml.Serialization.XmlRootAttribute(Namespace = "ConfigTest", IsNullable = false)]

public class statements

{

private select[] selectField;

private update[] updateField;

/// <remarks/

[System.Xml.Serialization.XmlElementAttribute("select")]

public select[] select

{

get

{

return this.selectField;

}

set

{

this.selectField = value;

}

}

/// <remarks/

[System.Xml.Serialization.XmlElementAttribute("update")]

public update[] update

{

get

{

return this.updateField;

}

set

{

this.updateField = value;

}

}

}

/// <remarks/

[System.SerializableAttribute()]

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "ConfigTest")]

public class select

{

private string idField;

private string resultMapField;

private string cacheModelField;

private string sqlField;

/// <remarks/

[System.Xml.Serialization.XmlAttributeAttribute()]

public string id

{

get

{

return this.idField;

}

set

{

this.idField = value;

}

}

/// <remarks/

[System.Xml.Serialization.XmlAttributeAttribute()]

public string resultMap

{

get

{

return this.resultMapField;

}

set

{

this.resultMapField = value;

}

}

/// <remarks/

[System.Xml.Serialization.XmlAttributeAttribute()]

public string cacheModel

{

get

{

return this.cacheModelField;

}

set

{

this.cacheModelField = value;

}

}

/// <remarks/

[System.Xml.Serialization.XmlTextAttribute()]

public string sql

{

get

{

return this.sqlField;

}

set

{

this.sqlField = value;

}

}

}

/// <remarks/

[System.SerializableAttribute()]

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "ConfigTest")]

public class update

{

private string idField;

private string parameterMapField;

private string sqlField;

/// <remarks/

[System.Xml.Serialization.XmlAttributeAttribute()]

public string id

{

get

{

return this.idField;

}

set

{

this.idField = value;

}

}

/// <remarks/

[System.Xml.Serialization.XmlAttributeAttribute()]

public string parameterMap

{

get

{

return this.parameterMapField;

}

set

{

this.parameterMapField = value;

}

}

/// <remarks/

[System.Xml.Serialization.XmlTextAttribute()]

public string sql

{

get

{

return this.sqlField;

}

set

{

this.sqlField = value;

}

}

}

}

3.实现IConfigurationSectionHandler接口

#region Using directives

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Reflection;

using System.Configuration;

using System.Xml;

using System.Xml.Serialization;

using System.Xml.Schema;

#endregion

namespace ConfigDll

{

public class MyConfigHandler : IConfigurationSectionHandler

{

private Type _configType = typeof(statements);

private string _schemaResourceName = "ConfigDll.MyConfig.xsd";

private string _schemaNamespace = "ConfigTest";

public MyConfigHandler()

{

}

public object Create(object parent, object configContext, System.Xml.XmlNode section)

{

XmlSerializer ser = new XmlSerializer(_configType);

// Create the XmlSchemaSet class.

XmlSchemaSet sc = new XmlSchemaSet();

// Add the schema to the collection.

Stream schemaStream = Assembly.GetAssembly(_configType).GetManifestResourceStream(_schemaResourceName);

sc.Add(_schemaNamespace, new XmlTextReader(schemaStream));

// Set the validation settings.

XmlReaderSettings settings = new XmlReaderSettings();

settings.XsdValidate = true;

settings.Schemas = sc;

settings.ValidationEventHandler += this.ValidationEventHandle;

XmlReader reader = XmlReader.Create(XmlReader.Create(new StringReader(section.OuterXml)), settings);

return ser.Deserialize(reader);

}

public void ValidationEventHandle(object sender, ValidationEventArgs args)

{

Console.WriteLine("\t验证错误:" + args.Message);

}

}

}

当然你可以建一个通用的校验类,这里只为演示

如果使用VS2003,校验需要使用XmlValidatingReader类,考虑到它在.NET2.0中已经过时所以使用新的方式

4.在App.Config中使用自定义配置节点

<?xml version="1.0" encoding="utf-8" ?

<configuration

<configSections

<section name="statements" type="ConfigDll.MyConfigHandler,ConfigDll" /

</configSections

<stat

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有