How do I make a C# class with a dictionary-like member using XSD?
Question
I'm using xsd.exe to make the C# classes for our settings. I have a setting that is per-server and per-database, so I want the class to behave like Dictionary<string, string[][]>. So I want to be able to say
string serverName = "myServer";
int databaseId = 1;
FieldSettings fieldSettings = getFieldSettings();
string[] fields = fieldSettings[serverName][databaseId];
how do I represent that in XSD?
Solution
xsd defines the data structure, not really the access approach. I don't think you can express "this is a lookup" in xsd : everything is either values or set of values/entities.
If you want specific handling, you might consider custom serialization - or alternatively consider your DTOs and your working classes as separate things, and simply translate between the two.
[edit] As leppie notes - you can write your own indexer (typically in a partial class if generating cs from the xsd) that loops over the list to find the item: this will give you the caller usage, but not really the full dictionary experience (uniqueness, O(1) retrieval, etc)
OTHER TIPS
This not what xsd
is used for. You can always just add your own indexer to a partial class, and mark it with [XmlIgnore]