views:

304

answers:

3

Does anyone know how to generate an XSD using LinqToXml? I can't find any examples of this anywhere. The XSD will be of the following fairly low level of complexity:

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 6.1.18.0 - FREE Community Edition (http://www.liquid-technologies.com)--&gt;
<xs:schema 
    elementFormDefault="qualified" 
    targetNamespace="http://schemas.xxx.yy/CRM/2009/01/DeadAnimalReport" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;

    <xs:element name="Name">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:length value="35" />
        </xs:restriction>
    </xs:simpleType>
    </xs:element>

    <xs:element name="Email" type="xs:string" />

    <xs:element name="Selection">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:length value="15" />
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

    <xs:element name="DeliveryDate" type="xs:date" />
</xs:schema>

The context construction of tooling to allow business analysts to generate message schemas along with some related artefacts that are out of scope of the question. The tooling XSD will be generated from CLR objects in the application's object model.

The objects are pretty simple - a root object that contains enough information to construct the namespace along with a collection of other objects representing the elements (type, name, etc).

Thanks

Sean

+1  A: 

Why do you want to use LINQ in this scenario? How does the source data look like?

Not much information given but anyway:

You can construct your XSD using similar code:

XNamespace nsXS = "http://www.w3.org/2001/XMLSchema";
XElement root = new XElement(nsXS + "schema",
    new XAttribute("elementFormDefault", "qualified"),
    new XAttribute("targetNamespace", "http://schemas.xxx.yy/CRM/2009/01/DeadAnimalReport"),
    new XElement(nsXS + "element",
        new XElement(nsXS + "simpleType",
            new XElement(nsXS + "restriction",
                new XAttribute("base", "xs:string")),
                new XElement(nsXS + "length", new XAttribute("value", 35)))));

If you have some sort of objects, then you can use projections:

var q =
    new XElement(nsXS + "schema",
                 from s in someObjects
                 select GetXsdDefinition(s)
        );

where

GetXsdDefinition is a method that takes your object as an argument and returns it's XSD definition

aku
Aku, I've added some context to the question.
Sean Kearon
But you didn't answer my questions. How can I help you if you don't even want to say what the source data is.
aku
Hi aku - I've added the source object structure to the end of the question.
Sean Kearon
Thanks aku, that's real close but I'm getting a failure on the xs:string and it doesn't have the xmlns:xs="http://www.w3.org/2001/XMLSchema attribute. Is is possible to get these in there too?
Sean Kearon
@Sean Kearon, AFAIK you can't force Linq2Xml to add "xs:" prefix to tags, but this is not a problem because you'll get a valid xsd anyway - there is no namespace disambiguation (if you add additional NS Linq2Xml would add "xs:" or other prefix to keep document valid)
aku
regarding "xs:string" I'm not sure what kind of failure you have, please provide more information
aku
aku - my apologies, I have just run your answer above and it works perfectly! Thanks for your help on this :)
Sean Kearon
A: 

There is also a LINQ to XSD, maybe thats what you`re looking for! You can find it HERE

regards, cordell

cordellcp3
Thanks David, but as far as I can tell LinqToXsd is about getting type safe objects back from XSDs, not generating an XSD.
Sean Kearon
Oops, sorry - I meant "thanks cordellcp3"!!
Sean Kearon
A: 

Since you want to use LinqToXml, I assume your scenario is that you already have some Xml and you want an Xsd to go with it.

LinqToXml doesn't really have much to do with Xsd's...

You may want to look at Xsd Inference tools.

David B
I have no XML, just CLR objects from the application. I've updated the question to indicate as such. An interesting link in any case!
Sean Kearon