an attribute argument must be a constant value - XmlRoot Namespace
I have the following code, which uses a struct to declare a const value to be used as the namespacce for the XmlRoot attribute, as we all know we can only have const values for at开发者_StackOverflow中文版tributes.
public struct Declarations
{
public const string SchemaVersion = "http://localhost:4304/XMLSchemas/Request.xsd";
}
[XmlRoot(ElementName = "Header", Namespace = Declarations.SchemaVersion, IsNullable = false), Serializable]
public class RequestHeader
{
...
This obviously throws the error 'an attribute argument must be a constant value. My question is, is there any way that i can use a value specified in the web.config, so that the namespace can be different for all the different environments i have - DEV, STE, UAT etc.
Thanks in advanced.
No, constant values must be present at compile time. This means that configuration file values can never be valid candidates for a constant in your code.
You could do something like this in conjunction with DEV
, STE
, and UAT
symbols (ugly, yes, but it would work):
public struct Declarations
{
public const string SchemaVersion_DEV
= "http://localhost:4304/XMLSchemas/Request.xsd";
public const string SchemaVersion_STE
= "http://someotherserver/XMLSchemas/Request.xsd";
public const string SchemaVersion_UAT
= "http://anotherserver/XMLSchemas/Request.xsd";
}
#if DEV
[XmlRoot(ElementName = "Header", Namespace = Declarations.SchemaVersion_DEV, IsNullable = false), Serializable]
#elif STE
[XmlRoot(ElementName = "Header", Namespace = Declarations.SchemaVersion_STE, IsNullable = false), Serializable]
#elif UAT
[XmlRoot(ElementName = "Header", Namespace = Declarations.SchemaVersion_UAT, IsNullable = false), Serializable]
#endif
public class RequestHeader { }
精彩评论