如何从app.config读取自定义XML?

Jim*_*mmy 9 .net c# windows-services app-config

我想从app.configC#windows服务中读取自定义XML部分.

我该怎么办呢?

XML如下:

<Books>
<Book name="name1" title="title1"/>
<Book name="name2" title="title2"/>
</Books>
Run Code Online (Sandbox Code Playgroud)

Kev*_*bet 14

在我开发的项目中,我使用了类似于我发现的配置.我相信这篇文章被称为我需要的最后一个配置部分处理程序(我找不到工作链接,也许有人可以为我链接它).

此方法将您想要做的更进一步,并实际将对象反序列化到内存中.我只是从我的项目中复制代码,但如果你想要的只是XML,那么倒退就应该相当简单.

首先,您需要定义一个处理配置设置的类.

using System;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;


namespace Ariel.config
{
    class XmlSerializerSectionHandler : IConfigurationSectionHandler
    {

        #region IConfigurationSectionHandler Members

        public object Create(object parent, object configContext, XmlNode section)
        {
            XPathNavigator nav = section.CreateNavigator();
            string typename = (string)nav.Evaluate("string(@type)");
            Type t = Type.GetType(typename);
            XmlSerializer ser = new XmlSerializer(t);
            return ser.Deserialize(new XmlNodeReader(section));
        }

        #endregion
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,假设您要加载一部分配置...超级简单,转换为您期望XML序列化的对象类型,并传递您正在寻找的部分(在这种情况下SearchSettings.

try
{
  config = (Eagle.Search.SearchSettings)ConfigurationSettings.GetConfig("SearchSettings");
}
catch (System.Configuration.ConfigurationException ex)
{
  syslog.FatalException("Loading search configuration failed, you likely have an error", ex);
  return;
}
Run Code Online (Sandbox Code Playgroud)

现在,您只需要App.config文件即可.我选择将我分成单独的文件(每个部分1个文件),以便更轻松地管理配置.您可以定义一个部分,为其命名,并定义类型(无论您将上面列出的类称为什么)和程序集的名称.

App.config中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="SearchSettings" type="Ariel.config.XmlSerializerSectionHandler, Ariel"/>
  </configSections>
  <SearchSettings configSource="Config\Search.config" />
</configuration>
Run Code Online (Sandbox Code Playgroud)

现在,剩下的就是要反序列化的配置文件.这里重要的是该块与您的节名称匹配,您的类型是它应该反序列化的任何对象,以及程序集名称.

<?xml version="1.0" encoding="utf-8" ?>
<SearchSettings type="Eagle.Search.SearchSettings, Eagle">
  <NumThreads>4</NumThreads>
</SearchSettings>
Run Code Online (Sandbox Code Playgroud)

如果您只想要纯原始XML,那么您需要做的就是修改处理该部分的Object以返回XML或执行您需要做的任何事情.


Col*_*kay 5

您想要做的是阅读自定义配置部分.

  • 祝您有足够勇气做到这一点的新手有个好运!对于新开发人员,他们可能会感到非常困惑。 (2认同)
  • 确实,你实现接口并使用XML的.NET 1.x方式更容易理解,IMO. (2认同)
  • 我发现这篇关于codeproject(http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx)的系列文章是对该主题的一个很好的解释. (2认同)

Tam*_*ely 5

由于IConfigurationSectionHandler已弃用,我认为值得一提的是,您仍然可以通过覆盖ConfigurationSection.DeserializeSection而不调用基本实现来实现纯序列化部分。

这是一个我经常重复使用的非常基本的示例。从内联 XAML 加载对象图的简单配置部分。(当然你可以用代替来实现XmlSerializer

using System.Configuration;
using System.Xaml;
using System.Xml;

...

public class XamlConfigurationSection<T> : ConfigurationSection
{
    public static XamlConfigurationSection<T> Get(string sectionName)
    {
        return (XamlConfigurationSection<T>)ConfigurationManager.GetSection(sectionName);
    }

    public T Content { get; set; }

    protected override void DeserializeSection(XmlReader xmlReader)
    {
        xmlReader.Read();
        using (var xamlReader = new XamlXmlReader(xmlReader))
            Content = (T)XamlServices.Load(xamlReader);
    }
}
Run Code Online (Sandbox Code Playgroud)