|
an automatic C# code generator for your .NET XML data classes
|
Last modified: June 16, 2008 |
NewsJune 16, 2008 November 6, 2007 May 9, 2006 March 31, 2006 March 29, 2006 March 4, 2006 February 25, 2006 February 4, 2006 February 3, 2006 |
DocumentationTo use ADCG, you must define the wanted data structure: you can do this in two ways:
1. Define your data structure using an SDF fileThe xml schema for this format of file is available
here.
Take a look at this SDF file , that describes a typical structure of personal infomation:
The root tag, <adcg>, allows some general control parameters:
The <classes> element contains a list of the
different classes of the data structure, it also defines the namespace to be
used for the classes.
Every <class> element defines a complex type of the data structure that will be mapped to a C# class.
Inside the class element, the tags <property> defines the list of the properties of the class. Every property could be of a standard type (see the table below for a list of the C# standard types supported) or another complex type.
2. Define your data structure using an XSD file
ADCG does not support all features of XDS technology. Supported XSD ElementsSimple Elements and Attributes declarations, also with restrictions, like: <xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> </xs:element>If a restriction is used, the type is mapped into a simple "string" type, without keeping "restrictions". Element type referencing is also supported: <xs:element ref="lastnameType"/>where lastnameType is declared as a global element type declaration. Complex Elements declaration are supported, <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element type="xs:string" name="firstname"></xs:element> <xs:element type="xs:string" name="lastname"></xs:element> </xs:sequence> </xs:complexType> </xs:element>but ADCG makes no difference between xs:sequence, xs:choice or xs:all. Not supported XSD ElementsOther often used xsd elements but not yet supported:
ADCG ExtensionsSome extension for ADCG is supported, for:
All these attributes must be declared under namespace http://www.codetools.it/adcg. 3. Run the toolTo run the tool you can use the Test Page, or download the program and run ADCG from the command line: ADCG <sdfFilePath> <outputFolder> <sdfFilePath>: the path of the structure descriptor file
(SDF or XSD file, see paragraph above).
Running the tool with the example SDF file described in this documentation, these are the output .cs files
generated 4. Using of the generated classesBelow there is an example on how use the classes and how to make a correct serialization. Note: if you use the code with .NET 1.1, it does not support XmlRootAttribute
when the class implements IXmlSerializable.
For .NET 1.1 you have to call CreateXmlSerializer(), a method exposed
by the generated classes to obtain a correct XmlSerializer instance to make a correct serialization when using customized tag names.
//Create the rubric object
Rubric r = new Rubric();
//Create a new person
Person p = new Person();
p.Name = "Christian";
p.Surname = "Salvaneschi";
p.Gender = true;
p.BirthDate = new DateTime(1975,7,15);
//Create a new address
Address a = new Address();
a.Street = "P.za Aldo Moro";
a.City = "Casarza Ligure";
a.ZIPCode = 16030;
a.State = "Genova";
a.Country = "Italy";
//Attach this address as the main address of the person
p.MainAddress = a;
//Add the person into the rubric
r.People.Add(p);
//Schema generation
XmlSchema s = r.GetSchema();
XmlTextWriter writer = new XmlTextWriter("c:\\MySchema.xsd", Encoding.ASCII);
writer.Formatting = Formatting.Indented;
s.Write(writer);
writer.Close();
//Xml Serialization
//For .NET 1.1, get the XmlSerializer from the rubric object
XmlSerializer serializer = r.CreateXmlSerializer();
//For .NET 2.0, create a normal serializer
XmlSerializer serializer = new XmlSerializer(typeof(Rubric));
writer = new XmlTextWriter("c:\\MySerialization.xml", System.Text.Encoding.ASCII);
writer.Formatting = Formatting.Indented;
serializer.Serialize(writer, r);
writer.Close();
Using the code above, these are the result files: |
| Copyright © 2006 Christian Salvaneschi. All Rights reserved. |