1

这是我的xml结构:

<reco>
    <styleSheets>
      <group>
        <asset source="~/Script/file1.css"/>
        <asset source="~/Script/file2.css"/>
        <asset source="~/Script/file3.css"/>
    </group>
  </styleSheets>
  <scripts>
    <group>
        <asset source="~/Content/file1.js"/>
        <asset source="~/Content/file1.js"/>
        <asset source="~/Content/file1.js"/>
    </group>
  </scripts>

这是我的代码:

public class AssetConfigurationElement : ConfigurationElement
{

    /// <summary>
    /// Gets or sets the source.
    /// </summary>
    /// <value>The source.</value>
    [ConfigurationProperty("source", IsRequired = true, IsKey = true)]
    public string Source
    {
        get
        {
            return (string)this["source"];
        }

        set
        {
            this["source"] = value;
        }
    }
}

public class GroupConfigurationElementCollection : ConfigurationElementCollection
{
    public GroupConfigurationElementCollection()
    {
        AddElementName = "asset";
    }

    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    /// <value>The name.</value>
    [ConfigurationProperty("name", IsRequired = true, IsKey = true, IsDefaultCollection = true)]
    public string Name
    {
        get
        {
            return (string)this["name"];
        }

        set
        {
            this["name"] = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is enabled.
    /// </summary>
    /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("enabled", DefaultValue = true)]
    public bool Enabled
    {
        get
        {
            return (bool)this["enabled"];
        }

        set
        {
            this["enabled"] = value;
        }
    }

    /// <summary>
    /// Gets or sets the version.
    /// </summary>
    /// <value>The version.</value>
    [ConfigurationProperty("version")]
    public string Version
    {
        get
        {
            return (string)this["version"];
        }

        set
        {
            this["version"] = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is compress.
    /// </summary>
    /// <value><c>true</c> if compress; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("compress", DefaultValue = true)]
    public bool Compress
    {
        get
        {
            return (bool)this["compress"];
        }

        set
        {
            this["compress"] = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is combined.
    /// </summary>
    /// <value><c>true</c> if combined; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("combined", DefaultValue = true)]
    public bool Combined
    {
        get
        {
            return (bool)this["combined"];
        }

        set
        {
            this["combined"] = value;
        }
    }


    protected override ConfigurationElement CreateNewElement()
    {
        return new AssetConfigurationElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((AssetConfigurationElement)element).Source;
    }

}

public class SharedGroupConfigurationSection : ConfigurationSection
{

    /// <summary>
    /// Gets the style sheets.
    /// </summary>
    /// <value>The style sheets.</value>
    [ConfigurationProperty("styleSheets")]
    [ConfigurationCollection(typeof(GroupConfigurationElementCollection), AddItemName = "group")]
    public GroupConfigurationElementCollection StyleSheets
    {
        get
        {
            return (GroupConfigurationElementCollection)base["styleSheets"] ?? new GroupConfigurationElementCollection();
        }
    }

    /// <summary>
    /// Gets the style sheets.
    /// </summary>
    /// <value>The style sheets.</value>
    [ConfigurationProperty("scripts")]
    [ConfigurationCollection(typeof(GroupConfigurationElementCollection), AddItemName = "group")]
    public GroupConfigurationElementCollection Scripts
    {
        get
        {
            return (GroupConfigurationElementCollection)base["scripts"] ?? new GroupConfigurationElementCollection();
        }
    }
}

这种配置甚至可能吗?如果是这样,我做错了什么?

当我尝试使用配置管理器获取该部分时,我收到此错误消息。

配置错误描述:处理服务此请求所需的配置文件期间发生错误。请查看下面的具体错误详细信息并适当地修改您的配置文件。

解析器错误消息:无法识别的元素“资产”。

源错误:

第 96 行: 第 97 行: 第 98 行: 第 99 行: 第 100 行:

源文件:D:\ASP.NET Projects\Resource-Compiler\ResourceCompiler\Examples\web.config 行:98

4

3 回答 3

0

应该是,但您需要为每个资产添加一个名称标签,因为它是每个资产的必需属性。如果您不需要它,则从中删除 IsRequired = true 和 IsKey = true 。ConfigurationElementCollection确实对 App.Config 中的数据格式提出了严格的要求。Sytem.Configuration 系统允许您从父 app.config 文件继承值。这甚至适用于对象集合。为了使其工作,MS 设计师引入了特殊标签。

为了从从父 app.config 加载的集合中删除所有元素,<clear/> 添加了标签。

要添加元素,您需要将<add/>数据序列化到属性中的标签。你的 app.config 应该看起来像

<add asset source="~/Script/file1.css"/>

要支持配置继承,您需要为必须遵守的完全锁定的序列化格式付出代价。当然,您可以扩展系统并添加您自己的配置提供程序,它的作用不同,但这不是一件容易的事。至少比使用像 XmlSerializer 这样的真正序列化程序复杂得多,在这种情况下,您对数据格式拥有最大的自由度。

DataContractSerializer 也不错,但它不允许您控制一切。XmlSerializer 仍然是 xml 文件最快和最通用的序列化器。

于 2012-05-06T06:39:56.533 回答
0

我能够让它工作。我必须为 styleSheet 节点和脚本节点添加两个新集合。这是我的完整代码:

    public class SharedGroupConfigurationSection : ConfigurationSection
{

    /// <summary>
    /// Gets the style sheets.
    /// </summary>
    /// <value>The style sheets.</value>
    [ConfigurationProperty("styleSheets", Options = ConfigurationPropertyOptions.IsRequired)]        
    public StyleSheetConfigurationElementCollection StyleSheets
    {
        get
        {
            return (StyleSheetConfigurationElementCollection)base["styleSheets"] ?? new StyleSheetConfigurationElementCollection();
        }
    }

    /// <summary>
    /// Gets the style sheets.
    /// </summary>
    /// <value>The style sheets.</value>
    [ConfigurationProperty("scripts", Options = ConfigurationPropertyOptions.IsRequired)]        
    public ScriptConfigurationElementCollection Scripts
    {
        get
        {
            return (ScriptConfigurationElementCollection)base["scripts"] ?? new ScriptConfigurationElementCollection();
        }
    }
}


[ConfigurationCollection(typeof(GroupConfigurationElementCollection), AddItemName = "group")]
public class ScriptConfigurationElementCollection : ConfigurationElementCollection
{

    protected override ConfigurationElement CreateNewElement()
    {
        return new GroupConfigurationElementCollection();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException();
        }

        return ((GroupConfigurationElementCollection)element).Name;
    }
}

[ConfigurationCollection(typeof(GroupConfigurationElementCollection), AddItemName="group")]
public class StyleSheetConfigurationElementCollection : ConfigurationElementCollection
{

    protected override ConfigurationElement CreateNewElement()
    {
        return new GroupConfigurationElementCollection();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException();
        }

        return ((GroupConfigurationElementCollection)element).Name;
    }
}

[ConfigurationCollection(typeof(AssetConfigurationElement), AddItemName = "asset", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class GroupConfigurationElementCollection : ConfigurationElementCollection
{
    public GroupConfigurationElementCollection()
    {
        AddElementName = "asset";
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new AssetConfigurationElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException();
        }

        return ((AssetConfigurationElement)element).Source;
    }

    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    /// <value>The name.</value>
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name 
    {
        get
        {
            return (string)base["name"];
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is enabled.
    /// </summary>
    /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("enabled", DefaultValue = true)]
    public bool Enabled
    {
        get
        {
            return (bool)this["enabled"];
        }

        set
        {
            this["enabled"] = value;
        }
    }

    /// <summary>
    /// Gets or sets the version.
    /// </summary>
    /// <value>The version.</value>
    [ConfigurationProperty("version")]
    public string Version
    {
        get
        {
            return (string)this["version"];
        }

        set
        {
            this["version"] = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is compress.
    /// </summary>
    /// <value><c>true</c> if compress; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("compress", DefaultValue = true)]
    public bool Compress
    {
        get
        {
            return (bool)this["compress"];
        }

        set
        {
            this["compress"] = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is combined.
    /// </summary>
    /// <value><c>true</c> if combined; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("combined", DefaultValue = true)]
    public bool Combined
    {
        get
        {
            return (bool)this["combined"];
        }

        set
        {
            this["combined"] = value;
        }
    }
}

public class AssetConfigurationElement : ConfigurationElement
{

    /// <summary>
    /// Gets or sets the source.
    /// </summary>
    /// <value>The source.</value>
    [ConfigurationProperty("source", IsRequired = false, IsKey = false)]
    public string Source
    {
        get
        {
            return (string)this["source"];
        }

        set
        {
            this["source"] = value;
        }
    }
}
于 2012-05-07T01:05:47.440 回答
0

正如我所看到的,您正在尝试将自定义部分插入到“标准”web.config 文件中。在这种情况下,您需要在

<configSections>

并在那里添加相应的部分。例如(这是我的一个项目中的一些 Quartz 配置):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>

        <sectionGroup name="common">
            <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        </sectionGroup>
        ... other sections here...
    </configSections>
    ... other web.config stuff here

然后,在下面的某个地方,您需要添加自己的部分,如下所示:

<common>
    <logging>
        <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">
            <arg key="configType" value="INLINE" />
        </factoryAdapter>
    </logging>
</common>

这是一篇关于此主题的 msdn 文章:http: //msdn.microsoft.com/en-us/library/2tw134k3.aspx

于 2012-05-06T05:25:52.947 回答