user1820988
user1820988

Reputation: 21

Using a .NET XMLWriter with XSD

I've been given the task of writing a complex XML file (I do have the XML schema, XSD) in C#, which has the possibility of being quite large depending on the situation. I'd like to implement streaming since the file can be large, so it looks like the best option is to use the XMLWriter. Before I go down the path of extending the XMLWriter class and writing a bunch of custom code, I was wondering if it was possible to, somehow, leverage the XML schema I have? I know I can convert my schema to C# objects using the XML Schema Definition Tool in Visual Studio, but I don't know if this is something I can use with the XMLWriter. I've converted an XML schema to C# objects and serialized them using XMLSerializer in the past, but not with the XMLWriter.

Upvotes: 2

Views: 3708

Answers (1)

David
David

Reputation: 21

See Generating XML Documents from XML Schemas http://msdn.microsoft.com/en-us/library/aa302296.aspx

Summary: Priya Lakshminarayanan shows how you can use the classes in the System.XML.Schema namespace of the Microsoft .NET Framework to build a tool that generates sample XML documents that conform to a given schema.

In the Visual Studio Schema Explorer you are able to generate an instance document from any element definition in your Schema. This article exposes the underlying code that makes that happen. I should note that Altova's XMLSpy has a more flexible tool for generating instances from the Schema, allowing you to set various parameters about the depth, repetition, and generated text values.

I used the XMLGenerator code included in the article to create a class that generates new XML document instances from my Schema for the 20 types of documents that we define. I added hints in my Schema as attributes in my own namespace to help the XMLGenerator generate a minimal valid document with some default text to help the users get started with the new document. So there is a lot you can do with the XmlGenerator.

Upvotes: 2

Related Questions