Create XSD from XML in Code

31,342

Solution 1

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>

Solution 2

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();
    }
}

Solution 3

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.

Share:
31,342
SharePointDummy
Author by

SharePointDummy

Updated on January 13, 2021

Comments

  • SharePointDummy
    SharePointDummy over 3 years

    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?