Sean Kearon
Sean Kearon

Reputation: 11427

Generate an XSD using LinqToXml

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)-->
<xs:schema 
    elementFormDefault="qualified" 
    targetNamespace="http://schemas.xxx.yy/CRM/2009/01/DeadAnimalReport" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <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

Upvotes: 0

Views: 802

Answers (3)

cordellcp3
cordellcp3

Reputation: 3623

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

Upvotes: 0

aku
aku

Reputation: 124044

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

Upvotes: 1

Amy B
Amy B

Reputation: 110221

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.

Upvotes: 0

Related Questions