无法使用ConfigurationBuilder从XML配置文件中解析任何值。

huangapple go评论63阅读模式
英文:

Cannot parse any Value from XML Configuration Files using ConfigurationBuilder

问题

我尝试使用IConfiguration/ConfigurationBuilder(Microsoft.Extensions.Configuration)从XML文件中加载配置值,但每次我想获取一个值时,我都只得到null

这是一个XML配置文件的示例:

<Application>
    <Configuration>
        <Modules>
            <SampleModule>
                <Detection>
                    <ScaleFactor>1.3</ScaleFactor>
                    <MinNeighbors>1</MinNeighbors>
                    <MinSize></MinSize>
                    <MaxSize></MaxSize>
                </Detection>
                <Overlay>
                    <Draw>true</Draw>
                    <Text>Face</Text>
                    <TextColor>
                        <A>0</A>
                        <R>255</R>
                        <G>0</G>
                        <B>0</B>
                    </TextColor>
                    <BoxColor>
                        <A>0</A>
                        <R>255</R>
                        <G>0</G>
                        <B>0</B>
                    </BoxColor>
                    <BoxBorderSize>1.5</BoxBorderSize>
                </Overlay>
                <UseEvents>true</UseEvents>
            </SampleModule>
        </Modules>
    </Configuration>
</Application>

这是我编写的用于调试问题的简单NUnit测试:

[Test]
public void TestConfigurationParsing() {

    ConfigurationBuilder builder = new();
    
    String configPath = Path.Join(Directory.GetCurrentDirectory(), "Configuration", "EyeDetection.xml");
    String xmlPath = "Application:Configuration:Modules:SampleModule:Overlay:Text";
    
    if (!File.Exists(configPath)) {
        throw new FileNotFoundException($"Could not find {configPath}");
    }

    builder.AddXmlFile(configPath);

    IConfigurationRoot config = builder.Build();
    
    Assert.IsNotNull(config[xmlPath]);
    Assert.That(config[xmlPath], Is.EqualTo("Face"));
}

这会失败。无论类型是什么(布尔值、Int32、字符串等),我都只得到null

关于路径:
我的应用程序位于
所有配置文件都位于./Configuration/*.xml

我做错了什么吗?我似乎无法弄清楚...

英文:

i'm trying to load Configuration Values from XML Files using the IConfiguration/ConfigurationBuilder (Microsoft.Extensions.Configuration). But everytime i want to get a Value, i just get null

This is one of the XML Configuration File

&lt;Application&gt;
    &lt;Configuration&gt;
        &lt;Modules&gt;
            &lt;SampleModule&gt;
                &lt;Detection&gt;
                    &lt;ScaleFactor&gt;1.3&lt;/ScaleFactor&gt;
                    &lt;MinNeighbors&gt;1&lt;/MinNeighbors&gt;
                    &lt;MinSize&gt;&lt;/MinSize&gt;
                    &lt;MaxSize&gt;&lt;/MaxSize&gt;
                &lt;/Detection&gt;
                &lt;Overlay&gt;
                    &lt;Draw&gt;true&lt;/Draw&gt;
                    &lt;Text&gt;Face&lt;/Text&gt;
                    &lt;TextColor&gt;
                        &lt;A&gt;0&lt;/A&gt;
                        &lt;R&gt;255&lt;/R&gt;
                        &lt;G&gt;0&lt;/G&gt;
                        &lt;B&gt;0&lt;/B&gt;
                    &lt;/TextColor&gt;
                    &lt;BoxColor&gt;
                        &lt;A&gt;0&lt;/A&gt;
                        &lt;R&gt;255&lt;/R&gt;
                        &lt;G&gt;0&lt;/G&gt;
                        &lt;B&gt;0&lt;/B&gt;
                    &lt;/BoxColor&gt;
                    &lt;BoxBorderSize&gt;1.5&lt;/BoxBorderSize&gt;
                &lt;/Overlay&gt;
                &lt;UseEvents&gt;true&lt;/UseEvents&gt;
            &lt;/SampleModule&gt;
        &lt;/Modules&gt;
    &lt;/Configuration&gt;
&lt;/Application&gt;

This is a Simple NUnit Test i wrote to debug the issue

    [Test]
    public void TestConfigurationParsing() {

        ConfigurationBuilder builder = new();
        
        String configPath = Path.Join(Directory.GetCurrentDirectory(), &quot;Configuration&quot;, &quot;EyeDetection.xml&quot;);
        String xmlPath = &quot;Application:Configuration:Modules:SampleModule:Overlay:Text&quot;;
        // /Application/Configuration/Modules/SampleModule/Overlay/Text
        
        if (!File.Exists(configPath)) {
            throw new FileNotFoundException($&quot;Could not find {configPath}&quot;);
        }

        builder.AddXmlFile(configPath);

        IConfigurationRoot config = builder.Build();
        
        Assert.IsNotNull(config[xmlPath]);
        Assert.That(config[xmlPath], Is.EqualTo(&quot;Face&quot;));
        // Assert.IsNotNull(config.GetValue&lt;String&gt;(xmlPath));
        // Assert.That(config.GetValue&lt;String&gt;(xmlPath), Is.EqualTo(&quot;Face&quot;));
    }

This Failes. I Just get null with every value. Regardless of Type (Boolean, Int32, String etc. )

Regarding the Paths:
My Application is in .
All the Configuration Files are in ./Configuration/*.xml

Is there something i'm doing wrong? I can't seem to figure it out...

答案1

得分: 1

配置系统会忽略< root >元素。因此,您的XML路径为

var xmlPath = "Configuration:Modules:SampleModule:Overlay:Text";

和值(应安装Microsoft.Extensions.Configuration.Binder)

var text = config.GetValue<string>(xmlPath); // "Face"

//或使用完整语法
text = config.GetSection(xmlPath).Get<string>();  // "Face"
英文:

The configuration system ignores the < root > element. So your xml path

var xmlPath = &quot;Configuration:Modules:SampleModule:Overlay:Text&quot;;

and value (Microsoft.Extensions.Configuration.Binder should be installed)

var text = config.GetValue&lt;string&gt;(xmlPath); // &quot;Face&quot;

//or a full syntax
text = config.GetSection(xmlPath).Get&lt;string&gt;();  // &quot;Face&quot;

huangapple
  • 本文由 发表于 2023年5月10日 19:25:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定