Question

Is it possible to use XSD.exe to auto generate a class which has a different datatype for an attribute than the one that is specified in the XSD by providing a annotation attribute before the element on the XSD.

The idea is, to keep the XSD just the way it is, but would like the auto generated class to have a different datatype for a specific attribute.

<xsd:simpleType name="idRestriction">
<Specify_Custom_Type="xsd:string" //This is what I'm looking for
<xsd:restriction base="xsd:decimal">

<xsd:attribute name="idAttribute" type="idRestriction" use="required" />

Generated class

[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal id { // Would like the decimal to be string
get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }

What I would like it to generate

[System.Xml.Serialization.XmlAttributeAttribute()]
public string id { // Notice decimal --> string
get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
Was it helpful?

Solution

Let's assume you have schema.xsd file

If you copy same file as schema2.xsd and replace all other primitive types to string, you can use this file for generating .cs class

If you automate this process by writing a batch file, i think your problem can be resolved.

Below link includes question about how to replace text file content. Please check that one

How can you find and replace text in a file using the Windows command-line environment?

I want to mention that there is one point when you generate .cs file from these files the class names will be the same. So it is better to use different name space for this both classes so the generated class types will not conflict.

You can mention namespace by using /n parameter for xsd.exe

Please check below link for xsd.exe command line parameter

http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx

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