英文:
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
<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>
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(), "Configuration", "EyeDetection.xml");
String xmlPath = "Application:Configuration:Modules:SampleModule:Overlay:Text";
// /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"));
// Assert.IsNotNull(config.GetValue<String>(xmlPath));
// Assert.That(config.GetValue<String>(xmlPath), Is.EqualTo("Face"));
}
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 = "Configuration:Modules:SampleModule:Overlay:Text";
and value (Microsoft.Extensions.Configuration.Binder should be installed)
var text = config.GetValue<string>(xmlPath); // "Face"
//or a full syntax
text = config.GetSection(xmlPath).Get<string>(); // "Face"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论