Is there a way to emulate using command line with the xsd.exe what happens when you click the 'Create Schema' option under the XML tab in Visual Studio?
I am trying to automate the process of creating schemas from XML in order to create classes using the XSD.
when I use the xsd file.xml command I get an error: Cannot add a column named 'MyXMLElement': a nested table with the same name already belongs to this DataTable. However, it works fine when I use the Create Schema option in VS2010.
<Person xmlns="http://someNamespace/1.0" xmlns:j="http://www.google.com/2.0" xmlns:nc="http://yahoo.com/3.0">
<MainElement>
<j:FirstElement>
<nc:SecondElement>
<nc:Value>something.com</nc:Value>
</nc:SecondElement>
</j:FirstElement>
<nc:FirstElement>
<ThirdElement>
<nc:PersonName>
<nc:Value>SomeName</nc:Value>
</nc:PersonName>
</ThirdElement>
</nc:FirstElement>
</MainElement>
</Person>
You can run
xsd.exe file.xml
and it should generatefile.xsd
, a schema for the instance document.I tried your edited sample and ran into the same error. I thought that approach worked but it seems I was wrong. As an alternative, .NET has the
System.Xml.Schema.XmlSchemaInference
class that I have tested not to throw an error:Of course instead of writing the schemas for testing to the console you could save them to files. There is one problem, in my test with your files three schemas are created in the schema set, one for each target namespace, and the main one does e.g.
<xs:import namespace="http://www.google.com/2.0" />
to import the other ones but as there are no files, theschema.Write
does not output any location.That is all I can currently suggest, I realize it is not a complete solution but perhaps it helps you solve the problem.