首页 新闻 搜索 专区 学院

在app.config或web.config中配置自定义的节点时

0
悬赏园豆:30 [已解决问题] 解决于 2015-10-30 17:30

如何做到像内置的节点一样有提示?

---------------------------------------------------------------------------------------

c#
happydaily的主页 happydaily | 小虾三级 | 园豆:674
提问于:2015-10-30 11:04
< >
分享
最佳答案
0

这个其实利用的是XSD文件,就是用XML Schema来描述XML结构,这里使用xsd来描述.config文件,在visual studio安装目录下\Xml\Schemas下面就是这些XSD文件,找到DotNetConfig40.xsd(.net 4.0)打开,你会发现appSettings、connectionStrings这些熟悉的节点,下面给个例子:

1.首先,自定义节点,如下:

namespace MidwareWatcherService.Config
{
    /// <summary>
    /// 受守护进程配置节点
    /// </summary>
    public sealed class WatcherConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("processes", IsDefaultCollection = true)]
        public ProcessElementCollection Processes
        {
            get { return (ProcessElementCollection)this["processes"]; }
            set { this["processes"] = value; }
        }
    }
    /// <summary>
    /// 进程配置元素集合
    /// </summary>
    public sealed class ProcessElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ProcessConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ProcessConfigurationElement)element).ID;
        }
        protected override string ElementName
        {
            get
            {
                return "process";
            }
        }
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
        public ProcessConfigurationElement this[int index]
        {
            get { return (ProcessConfigurationElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }
    }
    /// <summary>
    /// 进程配置元素
    /// </summary>
    public sealed class ProcessConfigurationElement : ConfigurationElement
    {
        /// <summary>
        /// 进程ID
        /// </summary>
        [ConfigurationProperty("id", IsKey = true, IsRequired = true)]
        public string ID
        {
            get { return (this["id"] ?? "").ToString(); }
            set { this["id"] = value; }
        }
        /// <summary>
        /// 进程名
        /// </summary>
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (this["name"] ?? "").ToString(); }
            set { this["name"] = value; }
        }
        /// <summary>
        /// 进程路径
        /// </summary>
        [ConfigurationProperty("path", IsRequired = true)]
        public string Path
        {
            get { return (this["path"] ?? "").ToString(); }
            set { this["path"] = value; }
        }
        /// <summary>
        /// 进程类型
        /// </summary>
        [ConfigurationProperty("type",DefaultValue = ProcessType.Normal, IsRequired = true),ProcessEnumValidator(typeof(ProcessTypeHelper))]
        public ProcessType Type
        {
            get
            {
                ProcessType _type;
                if (Enum.TryParse<ProcessType>(this["type"].ToString(), true, out _type))
                    return _type;
                else
                    return ProcessType.Normal;
            }
            set
            {
                this["type"] = value;
            }
        }
        /// <summary>
        /// 服务地址
        /// </summary>
        [ConfigurationProperty("serviceUri")]
        public string ServiceUri
        {
            get
            {
                return (this["serviceUri"] ?? "").ToString();
            }
            set
            {
                this["serviceUri"] = value;
            }
        }
        /// <summary>
        /// 重启时间间隔
        /// </summary>
        [ConfigurationProperty("restartInterval", DefaultValue = "00:00:00")]
        public TimeSpan RestartInterval
        {
            get { return (TimeSpan)this["restartInterval"]; }
            set { this["restartInterval"] = value; }
        }
        /// <summary>
        /// 重启时间,默认为凌晨2点
        /// </summary>
        [ConfigurationProperty("restartTime",DefaultValue="02:00:00")]
        public TimeSpan RestartTime
        {
            get { return (TimeSpan)this["restartTime"]; }
            set { this["restartTime"] = value; }
        }

        /// <summary>
        /// 是否启用
        /// </summary>
        [ConfigurationProperty("isEnabled", DefaultValue = true)]
        public bool IsEnabled
        {
            get { return (bool)this["isEnabled"]; }
            set { this["isEnabled"] = value; }
        }
    }
    /// <summary>
    /// 进程类型
    /// </summary>
    public enum ProcessType
    {
        Normal,
        WCF
    }
}
View Code

2.然后,添加一个xml架构文件(.xsd),如下:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vs="http://schemas.microsoft.com/Visual-Studio-Intellisense" elementFormDefault="qualified" attributeFormDefault="unqualified" vs:helpNamespace="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <xs:simpleType name="enum_ProcessType">
    <xs:restriction base="xs:NMTOKEN">
      <xs:enumeration value="Normal" />
      <xs:enumeration value="WCF" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="timespan_Type">
    <xs:restriction base="xs:string">
      <xs:pattern value="([0-9.]+:){0,1}([0-9]+:){0,1}[0-9.]+" />
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="processes">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="process">
          <xs:complexType>
            <xs:attribute name="id" type="xs:int" use="required"/>
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="path" type="xs:string" use="required" />
            <xs:attribute name="type" type="enum_ProcessType" use="optional" />
            <xs:attribute name="serviceUri" type="xs:string" use="optional" />
            <xs:attribute name="restartInterval" type="timespan_Type" use="optional" />
            <xs:attribute name="restartTime" type="xs:time" use="optional" />
            <xs:attribute name="isEnabled" type="boolean_Type" use="optional" />
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
View Code

3.然后,应用xsd文件。在vs中打开.config文件,VS菜单--XML--架构--添加自己创建的xsd--确定

4.最后,在.config文件中应用自定义节点,发现有提示了,如下图:

收获园豆:30
jello chen | 大侠五级 |园豆:7096 | 2015-10-30 15:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册