You can run xsd.exe file.xml
and it should generate file.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:
XmlSchemaInference xs = new XmlSchemaInference();
XmlSchemaSet schemaSet;
using (XmlReader xr = XmlReader.Create("file.xml"))
{
schemaSet = xs.InferSchema(xr);
}
foreach (XmlSchema schema in schemaSet.Schemas())
{
Console.WriteLine(schema.TargetNamespace);
schema.Write(Console.Out);
Console.WriteLine();
}
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, the schema.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.