Simon
Simon

Reputation: 25993

How do I make a C# class with a dictionary-like member using XSD?

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?

Upvotes: 1

Views: 1218

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1063814

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)

Upvotes: 2

leppie
leppie

Reputation: 117310

This not what xsd is used for. You can always just add your own indexer to a partial class, and mark it with [XmlIgnore]

Upvotes: 1

Related Questions