是否可以使用System.Configuration读取XML [英] Is it possible to read XML using System.Configuration

查看:93
本文介绍了是否可以使用System.Configuration读取XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用XElement解析XML并读取应用程序所需的每个节点.

Currently i am using XElement for parsing XML and read each node which required by application.

现在我想使用System.Configuration来读取XML.这是我想的吗.我的config.xml没有任何configuration section.它只是普通的XML.

Now i want to read XML by using System.Configuration.Is this possible, what i think.My config.xml have not any configuration section.It just plain XML.

<?xml version="1.0" encoding="utf-8" ?>
<ConfigSetting>
<!--The reports name which needs to be changed in to tethystrader db created on the fly.-->
<ReportsName value="Tethys_Price_report,Tethys_Liquidity_report,Tethys_Liquidity_report_option"/>

<MasterConnectionSetting connectionString="Data Source=NDI-LAP-262\SQL2008R2;Initial Catalog=master;UID=sa;pwd=Brick@123;" />
<!--Create db for check Liquidityreport/execta daily scenario-->
<Setup scenario="LIQ" outputFilePath="..\..\..\..\..\..\Branch_3.2.5">
    <ServerSetting>
        <ConnectionSetting component="RGTestToolDB" connectionString="server=NDI-LAP-262\SQL2008R2;integrated security=SSPI;uid=sa;pwd=Brick@123;database=~;Connection Timeout=1;" />
        <ConnectionSetting component="TethysTrader" connectionString="server=NDI-LAP-262\SQL2008R2;integrated security=SSPI;uid=sa;pwd=Brick@123;database=~;Connection Timeout=1;" />
        <ConnectionSetting component="TethysCommonDB" connectionString="server=NDI-LAP-262\SQL2008R2;integrated security=SSPI;uid=sa;pwd=Brick@123;database=~;Connection Timeout=1;" />      

    </ServerSetting>

    <DB component="TethysTrader">
        <cabfile path="Output\TethysTrader.cab" />
        <cabfile path="Output\TethysTrader-RG.cab" />
        <object  tablename="order_msgs" file="TethysTraderDB\order_msgs.csv" />
        <object  tablename="order_msgs_incoming" file="TethysTraderDB\order_msgs_incoming.csv" />
    </DB>

</Setup>

</ConfigSetting>

请提出建议.

推荐答案

因为这不是有效的配置文件(它不包含<configurationSettings>元素,也不包含用于描述未知部分的<configSections>元素),所以您无法阅读通过System.Configuration进行操作.通过将所有这些数据移动到自定义配置部分中,可以更好地实现您想要的(请参见 MSDN 有关如何执行此操作的更多信息-该页面讨论了ASP.NET,但它也可以在ASP.NET外部运行)或将这些数据保存在一个单独的文件中,并用XElement进行了解析(这没什么不对劲)该解决方案).

Because this is not a valid configuration file (it contains no <configurationSettings> element, nor a <configSections> element to describe unknown sections) you can't read it through System.Configuration. What you want is better achieved by either moving all of this data into a custom configuration section (see the MSDN for more information on how to do that -- the page talks about ASP.NET, but it works outside ASP.NET just as well) or keep this data in a separate file that you parse with XElement (there's nothing wrong with that solution).

使用自定义配置部分的好处是您可以保留一个app.config,这更易于维护和部署.您还可以从.NET配置文件的内置优点中受益,例如具有按计算机和按用户提供默认文件的功能.缺点是您需要为此编写单独的代码(并且了解其工作原理并不完全是简单的).

The benefit of using a custom configuration section is that you keep a single app.config, which is easier to maintain and deploy. You can also benefit from the built-in advantages of .NET configuration files, like the ability to have a per-machine and per-user file to supply defaults. The drawback is that you need to write separate code for it (and understanding how this works is not completely trivial).

将数据保存在您自己解析的单独文件中更容易理解,但是您没有获得针对计算机或用户特定文件的支持,并且如果需要一些设置,则必须部署和维护两个文件反正在app.config中.

Keeping the data in a separate file that you parse yourself is easier to understand, but you don't get the support for machine- or user-specific files, and you have to deploy and maintain two files if you need some settings in app.config anyway.

这篇关于是否可以使用System.Configuration读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆