1

I have a C# Application that communicates with the server using Json.

The server team has given me an xsd which I have used to generate C# classes with xsd2code.

While doing this, for each field in the xsd, xsd2code creates 2 fields in the generated C# classes. One with the same name as it is in xsd and another with the name suffixed with "Specified".

For example: Here's an xml from my xsd

<xsd:element name="depth" type="xsd:int" minOccurs="0"/>

Here's the corresponding fields that it generates.

private int depthField;

private bool depthFieldSpecified;

public int depth {
        get {
            return this.depthField;
        }
        set {
            this.depthField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool depthSpecified {
        get {
            return this.depthFieldSpecified;
        }
        set {
            this.depthFieldSpecified = value;
        }
    }

Is there a way to avoid getting the field suffixed with "Specified"? I need this to be removed because it is causing a problem when I try to serialize the object back to a json string. Even though I have populated the property depthField it doesn't serialize it by looking at depthFieldSpecified boolean which will be false by default.

I would be glad if someone can point me in the right direction. Thanks in Advance.

HelpMatters
  • 1,299
  • 1
  • 13
  • 32

1 Answers1

2

As far as I know you can't. The Specified properties have a very specific use for the XmlSerializer when it comes to serializing to XML again.

I have tried to 'fix' this myself in the Xsd2Code generator myself, but no luck so far. You could give it a try though by downloading the source from Xsd2Code from CodePlex.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks for your answer. I had one more follow up question. I only use JSON serialization and deserialization. Can I then safely remove all the fields and properties with the suffix "Specified" without facing any issues? – HelpMatters Sep 01 '14 at 13:32
  • Please read the linked article. It is neccesary I think if you use for example `int` that can be nullable according to your XML. Instead you could use `int?`. Then you don't need the `Specified` property. – Patrick Hofman Sep 01 '14 at 13:33
  • I have created another question specifically about json serialization and deserialization with the removal of all fields and properties with the suffix "Specified". You can find that question here http://stackoverflow.com/questions/25607237/can-i-safely-remove-the-fields-and-properties-with-the-suffix-specified-in-my-c – HelpMatters Sep 01 '14 at 13:49