Reputation: 303
I wish to read in an XML file using ConfigurationBuilder, but I keep getting an 'XML namespaces are not supported' Error. Is there a way to ignore the namespace? I'm fairly new to .net, so be gentle!
I'm trying to use ConfigurationBuilder to retrieve a connection string from an XML file, in order to access a cloudtable(It's a service fabric application, and it has to be an XML file, unfortunately) - It worked fine when I configured it for an 'appsetting.json' file, but this needs to change due to the way that someone else has set up the deployment configuration.
The XML follows the usual pattern e.g.:
<?xml version="1.0" encoding="utf-8" ?>
<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2011/01/fabric">
....etc.
The code that I'm using looks something like this:
string path = Path.Combine(FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config").Path);
IConfiguration cloudTableConfig2 = new ConfigurationBuilder()
.SetBasePath(path)
.AddXmlFile("Settings.xml", optional: true, reloadOnChange: true)
.Build();
The 'GetConfigurationPackageObject' is basically just to establish the correct path to the settings file, which is then passed to the Configuration builder.
I was hoping that it would be similar to appsettings.json, in that I could then parse the required values from the config in a similar fashion to the following:
IConfiguration cloudTableConfig = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
// Retrieve the storage account from the connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(cloudTableConfig["RequiredAzureTable:AzureConnectionString"]);
However, this doesn't work, as I get the following Exception Thrown at the ConfigurationBuilder stage:
System.FormatException: 'XML namespaces are not supported. Line 2, position 11.'
I can't really remove the namespace, as it's an externally configured file. Is there a simple way to tell the AddXmlFile method to ignore the namespace? Or can it only really be done by extending the AddXmlFile method? (Or rather, putting an extension method on IConfiguration that wraps around the AddXmlFile method?)
Upvotes: 0
Views: 2301
Reputation: 303
Turns out that I was overcomplicating it. Given that it was Service Fabric, all it needed was for me to put:
var azureConfig = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config");
From there on in it was just a simple case of retrieving the connection string like so (note: Could have combined these into one! Left them as is for simplicity):
var azureConfigSection = azureConfig.Settings.Sections["*SomeSectionName*"];
var azureDataTableConnectionString = azureConfigSection.Parameters["*ConnectionStringNameWithinSection*"].Value;
This allowed me to do the following:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azureDataTableConnectionString);
Upvotes: 1
Reputation: 69280
The namespace error is hard coded to be thrown on any namespace encountered. And the methods are private
so there is now way to simply override them.
I think you need to read the xml yourself and handle the properties you need. If it is a custom format file from an external source I would not read it without extra validation anyway.
If the file contains a lot of different properties that are not easily known in advance, it might be worth writing your own xml configuration provider. But I would try to avoid it and go for a simple hard coded approach if possible.
Upvotes: 1