Create XSD from XML in Code

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>

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

Tags:

C#

Xml

Xsd