Question

I need to define a field in an xsd file, that, when converted in C# classes using the XSD Tool, will turn into a const field (or something similar), for the purpose of not allowing any other values to be stored in the field.

At the moment, I have

<xs:attribute name="version" type="xs:string" use="required" fixed="01.01.01"/>

which is converted to a simple field with its associated property, and the parent's constructor will contain a definition to the respective value :

private string versionField;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute("mantec_assembly_order")]
public string refSchema 
{
     get 
     {
         return this.refSchemaField;
     }
     set 
     {
         this.refSchemaField = value;
     }
}

and the constructor

public Foo() 
{
    this.versionField = "01.01.01";
}

Is it possible to obtain a C# field with a fixed value, equal to the value inside the fixed attribute in the XSD file, by converting this file into a C# file using the XSD tool?

Thanks, Alex

Was it helpful?

Solution

Short answer is no.

As to make it a const (to allow, for e.g., to use it as a case label in a switch statement), for sure it is not going to work.

The reason has to do with the inner working of the XML serializer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top