SharePointDummy
SharePointDummy

Reputation: 357

Create XSD from XML in Code

I am using this piece of code from MSDN to create an XSD from an XML

XmlReader reader = XmlReader.Create("contosoBooks.xml");
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchemaInference schema = new XmlSchemaInference();

schemaSet = schema.InferSchema(reader);

foreach (XmlSchema s in schemaSet.Schemas())
{
   textbox.text = s.ToString();
}

I want to output the .xsd based on my xml file. When I generate the .xsd file, the only content I get inside it is: System.Xml.Schema.XmlSchema

When I generate the XSD using Visual Studio option to create Schema, it comes out properly. However, I have over 150 xml docs that I need to create XSD for hence need a programmatic option. Can anyone help?

Upvotes: 11

Views: 42229

Answers (5)

KSK
KSK

Reputation: 695

This is what you're missing... instead of simply doing s.ToString(), do this:

XmlWriter writer;
int count = 0;
foreach (XmlSchema s in schemaSet.Schemas())
{
    writer = XmlWriter.Create((count++).ToString() + "_contosobooks.xsd");
    s.Write(writer);
    writer.Close();
    Console.WriteLine("Done " + count);
}
reader.Close();

You can then write proper logic to do the read/write more gracefully, read many xml files and create corresponding xsd files, etc.

I took the contosobooks.xml from here: https://code.google.com/p/code4cs/source/browse/trunk/AppCase/dNet/Xml/data/contosoBooks.xml?spec=svn135&r=135

and the output xsd is:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bookstore">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="book">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element minOccurs="0" name="name" type="xs:string" />
                                        <xs:element minOccurs="0" name="first-name" type="xs:string" />
                                        <xs:element minOccurs="0" name="last-name" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="price" type="xs:decimal" />
                        </xs:sequence>
                        <xs:attribute name="genre" type="xs:string" use="required" />
                        <xs:attribute name="publicationdate" type="xs:date" use="required" />
                        <xs:attribute name="ISBN" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Upvotes: 9

masood
masood

Reputation: 9

Download microsoft visual studio 2010 or 2012.its very easy. Just click right on .xml file then choose microsft visual studio,then you will see xml tab, click then xml schema. It will generate xsd, save it to your local.

Upvotes: 0

L. Ben
L. Ben

Reputation: 11

I use this command line:

Administrator:Developer Command Prompt for VS2013

Create xsd file from xml:

xsd FullFilePath.xml /outputdir:myOutputDir

Generate object to represent that xsd file in our project.

xsd /c /n:myNameSpace FullPath\fileName.xsd

then it will tell where the .cs file is created.

Upvotes: 1

Guilherme Oliveira
Guilherme Oliveira

Reputation: 2026

xsd.exe can do what you want:

If you specify an XML file (.xml extension), Xsd.exe infers a schema from the data in the file and produces an XSD schema. The output file has the same name as the XML file, but with the .xsd extension.

The following command generates an XML schema from myFile.xml and saves it to the specified directory.

xsd myFile.xml /outputdir:myOutputDir

You can read more about it here and here

OR

You can try programmatically like this:

XmlReader reader = XmlReader.Create(@"yourxml.xml");
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchemaInference schema = new XmlSchemaInference();
schemaSet = schema.InferSchema(reader);

foreach (XmlSchema s in schemaSet.Schemas())
{
    using (var stringWriter = new StringWriter())
    {
        using (var writer = XmlWriter.Create(stringWriter))
        {
            s.Write(writer);
        }

        textbox.text = stringWriter.ToString();
    }
}

Upvotes: 19

Laoujin
Laoujin

Reputation: 10239

Use the command-line tool xsd.exe

Example usage:

nameOf.xsd /c /n:yourNamespace /out:C:\path\to\save

/c parameter creates a class (as opposed to /dataset which creates a strongly typed DataSet)

You can usually find this at: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin

And then use a batch file or Powershell to create all 150 classes.

Upvotes: 2

Related Questions