XsdClassesGen is a Custom Tool Add-In to VS.NET to generate type-safe wrapper classes for serializing to and from XML documents. It takes as input an XSD and produces the C# or VB.NET code to do the serialization using the XmlSerializer. This is really just the output of running xsd.exe /classes, but integrated directly into VS.NET.
To setup the XsdClassesGen custom tool, run the setup.bat file in the redist directory. It will export the types in XsdClassesGen.dll to COM and add the appropriate Registry entries for this custom tool.
To generate XmlSerializer classes using the XsdClassesGen custom tool add-in, you need an XSD file, e.g.
<?xml version="1.0" ?> <xs:schema id="NewDataSet" targetNamespace="http://tempuri.org/unityLogistics.xsd" xmlns:mstns="http://tempuri.org/unityLogistics.xsd" xmlns="http://tempuri.org/unityLogistics.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified"> <xs:element name="unity"> <xs:complexType> <xs:sequence> <xs:element name="currentModule" type="xs:int" minOccurs="0" /> <xs:element name="nextLecture" type="xs:string" minOccurs="0" /> <xs:element name="currentActivity" type="xs:string" minOccurs="0" /> <xs:element name="notes" minOccurs="0" maxOccurs="1"> <xs:complexType> <xs:sequence> <xs:element name="note" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Once you've created your XSD file, add the file to your project and set the Custom Tool property to "SBXsdClassesGenerator" (no quotes). After you've done that, and VS.NET notices, you'll get an associated .cs or .vb file. To see it, make sure that Show All Files is selected at the top of the Solution Explorer. From then on, whenever you change the contents of your XSD file, the code will be regenerated. When you build, it will be part of your project.
Once you've got the serialization types, you can now load and save XML data that conforms to the XSD using the XmlSerializer class in a type-safe, object-oriented manor, like so:
using( FileStream fs = new FileStream(@"..\..\unityLogistics.xml", FileMode.Open, FileAccess.Read) ) { XmlSerializer serializer = new XmlSerializer(typeof(unity)); unity u = (unity)serializer.Deserialize(fs); Console.WriteLine("currentModule= {0}", u.currentModule); Console.WriteLine("nextLecture= {0}", u.nextLecture); Console.WriteLine("currentActivity= {0}", u.currentActivity); Console.WriteLine("notes:"); foreach( string note in u.notes ) { Console.WriteLine("\tnote: {0}", note); } }
If you'd like to know more about what a custom tool is and how to build your own, check out CollectionGen.
Copyright © 2002-2003 Chris Sells
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications, subject to the following restrictions: