4

Is there any way to get automatic properties in my class file that's generated from xsd? I am using Xsd2code and have tried the following command.

c:\xsd2code  q2test.xsd /n ContractXml /pl Net35 /ap[+] /xa[+]

It doesn't generate automatic properties. It generates something like this:

        public string AssetHdrId {
            get {
                return this.assetHdrIdField;
            }
            set {
                this.assetHdrIdField = value;
            }
        }

private string assetHdrIdField;

I want something simple like public string AssetHdrId{get;set;}

I have around 355 properties in my class and I wanted to ask around before changing each one of them manually.

abhi
  • 3,082
  • 6
  • 47
  • 73

1 Answers1

2

This answer is late but might be useful for others if you are looking to generate properties for a class that do not contain backing fields using xsd2Code. First, let's define a backing field. A default Properties in C# .net 2.0 was created using a private variable and public property (where the C# keyword value is the incoming string value):

private string _loanId;
public string LoanId
{
    get{ return _loanId; }
    set{ _loanId = value; }
}

This is very verbose and as the question poster describes can really bloat a class. In C# 3.0, this changed and properties could be created without backing fields:

public string LoandId { get;set; }

I used xsd2Code++ V 4.2... and was able to set options that would enable property creation without a backing field.
Follow these steps:

  1. Install XSD2Code++ or XSD2Code Community Edition
  2. Right click on an .xsd file
  3. In the options panel, set the Application -> Target Framework to Net45
  4. In the options panel, set the Settings -> PropertyParams -> Automatic Properties to true.

If you have auto update set, you'll see the backing properties disappear and be left with a much less verbose class. You can also click the "generate" button to see the effects.

Cheers!

sondlerd
  • 1,025
  • 1
  • 10
  • 15