0

I've been assigned a task to import reference data from a database into a set of entity classes. Here's an example of one of the simpler entity classes:

public class CountryList
{
    public CountryList()
    {
        this.Countries = new List<Country>();           
    }

    public IList<Country> Countries { get; private set; }
}

Populating the XML files isn't my responsibility. But providing a schema (XSD file) in order to specify the required format is.

So far I've tried annotating the required classes/members with [DataContract]/[DataMember] and using svcutil to generate a set of XSD files using this command:

svcutil /t:metadata /dconly MyProject.dll

This generates a lot of stuff in 10 separate XSD files. I couldn't find a way of specifying only the entities I'm interested in and slimming it all down. (It is only some reference data that needs to be imported and there are plenty of entity classes that don't need to be in the XSD).

On an alternative tack, I found this article on how to deserialize from an XML file. Looks pretty simple on the face of it but I'm wondering how the XSD file for the book class in this example would be generated?

Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
  • This sounds a lot like a home-brewed ORM. Why is your database access being channeled through XML? – Spencer Ruport May 01 '13 at 08:13
  • We are working on a cloud application in Microsoft Azure. The cloud app is separate from the main database (and has its own table storage) but reference data from the main database needs to be uploaded to it periodically. – Steve Chambers May 01 '13 at 08:17

2 Answers2

1

To get a schema from a class or vice versa, use xsd.exe

http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.71).aspx

There is a lot of reading to do on how a complex class would translate into diferent schema, but you should get off to a decent start using that tool.

Mark Dickinson
  • 6,573
  • 4
  • 29
  • 41
  • I actually tried xsd.exe first but it complains about `IList` not being serializable. My understanding is a bit hazy here but isn't xsd.exe kind of like an older version of svcutil.exe? – Steve Chambers May 01 '13 at 08:19
  • 1
    Sounds about right, there are a lot of utilities that cause what is traditionally viewed as bad design due to IList not being serializable. Is it possible to expose a concrete List instead? This is viewed as bad form, but on an entity class it should be ok. If we were forced into doing this in order to work with rest sharp for example, the entity would actually be a DTO so the consequences of exposing a concrete List are mitigated, as we are only using the DTO as a bucket to pass data around in. Public lists are frowned upon as they can't tell calling classes if they change. – Mark Dickinson May 01 '13 at 08:35
1

It turned out to be a lot cleaner to generate an XSD using code (rather than using either xsd.exe or svcutil.exe). Here are the steps I followed:

  1. Create a new Console Application like this but using a StreamWriter to output to a file instead of the console. The resulting XSD has just the entities I need and no more. The [DataContract] / [DataMember] attributes cause XSD constructs to have the same names as in the code.

  2. Use Eclipse to generate sample XML from the XSD like this.

  3. Use code like this to deserialize the XML file into the required entities.

Community
  • 1
  • 1
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208