2

I know there are several answers on this board regarding .xsd scheme creation, but I found nothing that really fits what I'm trying to do.

I have a very simple XML file with nested elements etc. (but no namespaces or dtd) and I'm trying to generate a XSD file out of it. I reflected the code from Microsoft XSD.exe (which is being brought up very often when it comes to XSD generation) and saw a scheme generation is done by using XmlSchemaInference class (InferScheme() method).

Now, my XML file is already loaded using XDocument.Load() from the LINQ-namespace. But when I look at XmlSchemaInference, I see that this only takes an XmlTextReader as an xml document argument.

Do I get this right that I have to load my XML file for using with LINQ as an XDocument and then load it again with XmlTextReader to create a XSD? Or does XDocument provides similiar methods as it contains the extension method .Validate()?

I'm very confused when it comes to XML, as .net seems to provide hundreds of classes, but no one of them is able to provide full functionality. :/

What I'm trying to do in short:

  • Load XML file using XDocument (LINQ)
  • Create XSD scheme using native framework / XmlSchemeSet methods (no xsd.exe stuff) from XDocument
  • with new XDocument object, save using XDocument.Save()

What it seems I have to do:

  • Load XML file using XDocument
  • Load XML file again using XmlTextReader
  • Pass XmlTextReader to XmlSchemaInference
  • Pass new XmlTextWriter to XmlSchemaInference
  • Save document using XmlTextWriter

... pretty ridiculous, mh?

1 Answer 1

0

Well XmlSchemaInference.InferSchema takes any XmlReader as the input so assuming you have a LINQ to XML XDocument you create an XmlReader over that document with http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.createreader.aspx. Then, as XML documents in general can contain XML elements from different namespaces, the InferSchema method returns an XmlSchemaSet (as a schema is needed for each target namespace). With that XmlSchemaSet you have then all options to save the schema(s) to a file or if you want an XDocument you create one, using e.g.

List<XDocument> schemas = new List<XDocument>();
foreach (XmlSchema schema in schemaSet.Schemas())
{
  XDocument schemaDoc = new XDocument();
  using (XmlWriter xw = schemaDoc.CreateWriter())
  {
     schema.Write(xw);
  }
  schemas.Add(schemaDoc);
}

XmlTextWriter and XmlTextReader are legacy implementations of XmlWriter/XmlReader, they are best avoided unless you need some backwards compatible quirk parsing or writing of XML.

But in general the XmlReader and XmlWriter class are the ones used in the XML APIs of the .NET framework to provide the low level interaction between the different classes.

It is not clear from your description however if you want the schema(s) as XDocument instances, if you simply want to save the schema(s) to a file then you can directly call the schema.Write method for each schema on a file stream.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.