-->

Is it possible to read XML using System.Configurat

2019-07-31 23:33发布

问题:

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

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>

Please suggest.

回答1:

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).

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).

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.