RSS Feed (xml)

Create a Schema for a .NET Class

Previous Post demonstrated how to use the XmlSerializer to serialize .NET objects to XML, and deserialize XML into .NET objects. But if you want to use the XML as a way to interact with other applications, business process, or non-Framework applications, you'll need an easy way to validate the XML before you attempt to deserialize it. You'll also need to define an XML schema document that defines the structure and data types used in your XML format, so that other applications can work with it. One quick solution is to generate an XML schema using the xsd.exe command-line utility.

The xsd.exe utility is included with the .NET Framework. If you've installed Microsoft Visual Studio .NET, you'll find it in a directory like C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin. The xsd.exe utility can generate schema documents from compiled assemblies. You simply need to supply the filename and indicate the class that represents the XML document with the /t:[TypeName] parameter.

For example, consider the ProductCatalog and Product classes shown in this post. You could create the XML schema for a product catalog with the following command line:

xsd Recipe5-09.exe /t:ProductCatalog

You need to specify only the ProductCatalog class on the command line because this class represents the actual XML document. The generated schema in this example will represent a complete product catalog, with contained product items. It will be given the default filename schema0.xsd. You can now use the XmlValidatingReader shown in this post to test whether the XML document can be successfully validated with the schema.

Technorati :

No comments:

Post a Comment

Archives In Web World

Fun Mail - Fun in the Mail