Optional value-types in autogenerated XSD?

M

Martin Z

I'm using XSD.EXE to generate XSD from my classes. I've worked through
most of the kinks, but there's one thing that is still sending me for a
loop: is there a way to make value-types not "required"? I've got
numerous boolean fields for minor configuration tweaks, and since these
XML files are intended for manual editing, having all those
superfluous booleans be mandatory would be a tedious headache -
especially since the classes they represent are providing defaults
anyways.

I tried using enums, but the problem exists for all value types. I
tried using nullables, but then they'd have to be elements and not
attributes (and these XML files are designed to be convenient to
hand-edit, so the distinction is important). Hand-editing the XSD-file
sounds best, but I'm regenerating them often since I'm still actively
developing the config structure.

Any tips?
 
M

Marc Gravell

Place a DefaultValueAttribute on the properties, e.g.

private bool something = false;
[DefaultValue(false)]
public bool Something {
get {return something;} set {something = value;}
}

Now the schema will accept missing values, defaulting to the given
value.

Marc
 
M

Martin Z

Thanks, that's precisely what I was looking for. I wonder why that
isn't documented better - I looked all over for it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top