熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

.NET中自定義配置節點實例詳解

2022-06-13   來源: Java核心技術 

  目的
  一般應用都有自己的配置文件如何將配置文件映射到NET中的對象是有現實意義的事情在Java中有一個digester開源項目實現了這個功能下面我一步一步來說明NET中如何更簡單的實現他
  
  實現
  定義xsd架構文件我們定義幾個簡單的架構
  
  源文件如下
  <?xml version= encoding=utf?>
  <xs:schema id=MyConfig targetNamespace=ConfigTest elementFormDefault=qualified xmlns=ConfigTest xmlns:mstns=ConfigTest xmlns:xs=>
  <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= maxOccurs=unbounded />
  <xs:element name=update type=update minOccurs= maxOccurs=unbounded />
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>
  
  使用xsdexe生成對應於架構的配置類
  xsdexe MyConfigxsd /c
  
  //
  // <autogenerated>
  //   This code was generated by a tool
  //   Runtime Version:
  //
  //   Changes to this file may cause incorrect behavior and will be lost if
  //   the code is regenerated
  // </autogenerated>
  //
  
  using SystemXmlSerialization;
  
  //
  // This source code was autogenerated by xsd Version=
  //
  namespace ConfigDll
  {
  
  /// <remarks/>
  [SystemSerializableAttribute()]
  [SystemXmlSerializationXmlTypeAttribute(Namespace = ConfigTest)]
  [SystemXmlSerializationXmlRootAttribute(Namespace = ConfigTest IsNullable = false)]
  public class statements
  {
  
  private select[] selectField;
  
  private update[] updateField;
  
  /// <remarks/>
  [SystemXmlSerializationXmlElementAttribute(select)]
  public select[] select
  {
  get
  {
  return thisselectField;
  }
  set
  {
  thisselectField = value;
  }
  }
  
  /// <remarks/>
  [SystemXmlSerializationXmlElementAttribute(update)]
  public update[] update
  {
  get
  {
  return thisupdateField;
  }
  set
  {
  thisupdateField = value;
  }
  }
  }
  
  /// <remarks/>
  [SystemSerializableAttribute()]
  [SystemXmlSerializationXmlTypeAttribute(Namespace = ConfigTest)]
  public class select
  {
  
  private string idField;
  
  private string resultMapField;
  
  private string cacheModelField;
  
  private string sqlField;
  
  /// <remarks/>
  [SystemXmlSerializationXmlAttributeAttribute()]
  public string id
  {
  get
  {
  return thisidField;
  }
  set
  {
  thisidField = value;
  }
  }
  
  /// <remarks/>
  [SystemXmlSerializationXmlAttributeAttribute()]
  public string resultMap
  {
  get
  {
  return thisresultMapField;
  }
  set
  {
  thisresultMapField = value;
  }
  }
  
  /// <remarks/>
  [SystemXmlSerializationXmlAttributeAttribute()]
  public string cacheModel
  {
  get
  {
  return thiscacheModelField;
  }
  set
  {
  thiscacheModelField = value;
  }
  }
  
  /// <remarks/>
  [SystemXmlSerializationXmlTextAttribute()]
  
  public string sql
  {
  get
  {
  return thissqlField;
  }
  set
  {
  thissqlField = value;
  }
  }
  }
  
  /// <remarks/>
  [SystemSerializableAttribute()]
  [SystemXmlSerializationXmlTypeAttribute(Namespace = ConfigTest)]
  public class update
  {
  
  private string idField;
  
  private string parameterMapField;
  
  private string sqlField;
  
  /// <remarks/>
  [SystemXmlSerializationXmlAttributeAttribute()]
  public string id
  {
  get
  {
  return thisidField;
  }
  set
  {
  thisidField = value;
  }
  }
  
  /// <remarks/>
  [SystemXmlSerializationXmlAttributeAttribute()]
  public string parameterMap
  {
  get
  {
  return thisparameterMapField;
  }
  set
  {
  thisparameterMapField = value;
  }
  }
  
  /// <remarks/>
  [SystemXmlSerializationXmlTextAttribute()]
  public string sql
  {
  get
  {
  return thissqlField;
  }
  set
  {
  thissqlField = value;
  }
  }
  }
  }
  
  實現IConfigurationSectionHandler接口
  
  #region Using directives
  
  using System;
  using SystemCollectionsGeneric;
  using SystemText;
  using SystemIO;
  using SystemReflection;
  using SystemConfiguration;
  using SystemXml;
  using SystemXmlSerialization;
  using SystemXmlSchema;
  
  #endregion
  
  namespace ConfigDll
  {
  public class MyConfigHandler : IConfigurationSectionHandler
  {
  private Type _configType = typeof(statements);
  private string _schemaResourceName = ConfigDllMyConfigxsd;
  private string _schemaNamespace = ConfigTest;
  
  public MyConfigHandler()
  {
  }
  
  public object Create(object parent object configContext SystemXmlXmlNode section)
  {
  XmlSerializer ser = new XmlSerializer(_configType);
  
  // Create the XmlSchemaSet class
  XmlSchemaSet sc = new XmlSchemaSet();
  
  // Add the schema to the collection
  Stream schemaStream = AssemblyGetAssembly(_configType)GetManifestResourceStream(_schemaResourceName);
  scAdd(_schemaNamespace new XmlTextReader(schemaStream));
  
  // Set the validation settings
  XmlReaderSettings settings = new XmlReaderSettings();
  settingsXsdValidate = true;
  settingsSchemas = sc;
  settingsValidationEventHandler += thisValidationEventHandle;
  
  XmlReader reader = XmlReaderCreate(XmlReaderCreate(new StringReader(sectionOuterXml)) settings);
  return serDeserialize(reader);
  }
  
  public void ValidationEventHandle(object sender ValidationEventArgs args)
  {
  ConsoleWriteLine(\t驗證錯誤 + argsMessage);
  }
  
  }
  }
  當然你可以建一個通用的校驗類這裡只為演示
  如果使用VS校驗需要使用XmlValidatingReader類考慮到它在NET中已經過時所以使用新的方式
  
  在AppConfig中使用自定義配置節點
  <?xml version= encoding=utf ?>
  <configuration>
  <configSections>
  <section name=statements type=ConfigDllMyConfigHandlerConfigDll />
  </configSections>
  <stat
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27083.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.