Previous Post introduced the xsd.exe command-line utility, which can be used to generate schemas based on class definitions. The reverse operation-generating C# source code based on an XML schema document-is also possible. This is primarily useful if you want to write a certain format of XML document, but you don't want to manually create the document by writing individual nodes with the XmlDocument or XmlTextWriter classes. Instead, by using xsd.exe, you can generate a set of full .NET objects. You can then serialize these objects to the required XML representation using the XmlSerializer, as described in recipe 5.9.
To generate source code from a schema, you simply need to supply the filename of the schema document and add the /c parameter to indicate that you want to generate the required classes. For example, consider the schema shown in this post. You can generate C# code for this schema with the following command-line:
xsd ProductCatalog.xsd /c
This will generate one file (ProductCatalog.cs) with two classes: product and productCalalog.
No comments:
Post a Comment