Ram
Ram

Reputation: 11644

Create XSD for hierarchical data

I want to export objects to XML and create XSD. following are the base classes

class Owner
{
    private int id;
    private string name;
    private List<Car> cars;
    private int age;
}

class CarTemplate
{
    private int drive;
    private double engineCc;
}

class Car
{
    private int id;
    private string name;
    private double cost;
    private CarTemplate template;
}

Following is the modified structure that I want to export to XML and create XSD out of it

class ExportedOwner
{
    private int id;
    private string name;
    private List<Car> cars;
}

class ExportedCar
{
    private string name;
    private double cost;
    private CarTemplate template;
}

The structure of XML should be in following format

<?xml version="1.0" encoding="utf-8" ?>
<Owner>
    <id>1</id>
    <Name>John</Name>
    <Age>49</Age>
    <Cars>
        <Car>
            <id>1</id>
            <Name>Merc Class C</Name>
            <CarTemplate>
                <drive>2</drive>
                <engineCc>2500</engineCc>
            </CarTemplate>
        </Car>
        <Car>
            <id>2</id>
            <Name>Merc Class M</Name>
            <CarTemplate>
                <drive>4</drive>
                <engineCc>2900</engineCc>
            </CarTemplate>
        </Car>
    </Cars>
</Owner>

How can I create XSD for the XML format. I want XSD for importing the XML file back into the appication.

Upvotes: 0

Views: 932

Answers (4)

Alex
Alex

Reputation: 8116

If you want to generate the XML out of your classes dynamically, you could use System.Reflection

For example:

Type type = typeof(ExportedOwner);

var propertyinfos = type.GetProperties(); // gives you a list of all properties of ExportedOwner

And then generate your XML according to your specification with your favourite XMl Writer.

Upvotes: 0

MattDavey
MattDavey

Reputation: 9017

There's 2 distinct questions to answer here...

First, how to export your objects to an XML file. There are many ways to do this in .NET. The two most common (XmlSerializer and DataContractSerializer) are compared side by side in this blog post.

The second part of your question is a little more broad. Generating an xsd schema from an existing XML file is not an exact science, but there are tools out there that can infer (or guess) a schema from an XML file. There are various little tools which can do this for you - it might be worth trying a few of them and seeing which one gives you best results. As always, google is your good friend :)

Upvotes: 0

deadlyvices
deadlyvices

Reputation: 883

Use XSD.EXE for generating your XSD: it's a standard .NET Framework tool. See http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx to see how it is used.

Upvotes: 1

Davin Tryon
Davin Tryon

Reputation: 67296

You can use the standard XML serializer for this (with xsd.exe). You would need to use the KnownTypeAttribute on the base class in order for the deserialized to know how to rehydrate the structure.

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.knowntypeattribute.aspx

Upvotes: 0

Related Questions