Generate classes with xsd.exe
All we need is Visual Studio 2005 and a database, for this example I’m creating a new class called Customer which is based on the database table Customer, the table columns description is shown below.
1) Open your project and create a new empty XML Schema, for this example.
2) Open the server explorer in visual studio 2005 and connect to your database server, for this example I’m using a database located in my pc.
3) Drag the database table into your empty XML schema previously created.
4) Modify the schema as wanted, for this example I’m making the following changes:
· Changing the file id from an element to an attribute
· Removing the main element called Document, leaving just one main element called Customer.
You can also add subtypes, for example dragging a “telephoneNumber” table which is not showed in the example, but contains the following fields: Id, Number and type. Create a new type called Telephone, make some modifications to it and set it as reference for the fields MobileNumber, PhoneNumber and FaxNumber in our main “class” (Customer) XML schema. On that way you’ll build a more structured class.
At the end the schema looks like this:
5) Look into your PC for the application xsd.exe it should be in the directory: C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin
(Optional) It is easier if you copy the xsd.exe file and your XML schema .xsd file in a known directory in your pc.
6) Open a command console and browse to the directory where the XML schema file and the file xsd.exe are in and run the following command:
xsd.exe -c -l:c# -n:[namespace] [XML Schema Name].xsd
For this example the command is:
xsd.exe -c -l:c# -n:Customer Customer.xsd
For more information on the xsd.exe tool visit: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpconxmlschemadefinitiontoolxsdexe.asp
7) Once you have done this, you will get a new file in the same directory, that file is the class!!! So basically you are done now, is just copy that file into your App_code directory or wherever you want.
8) What’s the big deal with this? The big deal is that now you can modify your class, add namespaces or whatever, and just creating an instance and adding a couple lines, you can use the XmlSerializer Class to build an XML document from your class, which will follow the same format as the XML schema that you created at the beginning, making easier validation and such processes. There are a lot more of things that you can do, this is just an example, for more information visit:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlserializationxmlserializerclasstopic.asp
You will notice that in the new class you’ll get some properties named like this: [existing property]+Specified. These properties are used by the XmlSerializer class to omit properties with “non nullable” types like int, double, etc. when none value has been assigned to the property. You can just delete those fields, but you have to be aware that when you make an instance of the class and try to serialize it you will get lines like this one: “0,00” , supposing defaultCommission hadn’t assigned any value in your instance.
Here is a simple example of the serializing process.
Add this method to the class Customer:
public string ToXml()
{
XmlSerializer serializer = new XmlSerializer(typeof(Customer));
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.IO.TextWriter writer = new System.IO.StreamWriter(stream);
//This line builds the xml into the stream
serializer.Serialize(writer, this);
//The following lines are used just to return a string but the XML is already build, you can convert it into a file or whatever you want.
writer.Flush();
stream.Seek(0, System.IO.SeekOrigin.Begin);
System.IO.TextReader reader = new System.IO.StreamReader(stream);
stream.Flush();
string strXml = reader.ReadToEnd();
stream.Dispose();
writer.Dispose();
reader.Dispose();
return strXml;
}
and with a test page containing the following test:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e) {
Customer.Customer cust = new Customer.Customer();
cust.Address = “Address line number 1″;
cust.City = “Any city”;
cust.Country = “Any COuntry”;
cust.FaxNumber = “2843023984″;
cust.FirstName = “customer’s First name”;
cust.Id = 123;
cust.IdSpecified = true;
cust.PhoneNumber = 230984209843;
TextBox1.Text = cust.ToXml();
}
The result obtained is as follows:
As you notice the Id property is inserted as defined, as an attribute, and the property PhoneNumber is not shown even that a value was assigned to it, this is because the property PhoneNumberSpecified wasn’t set to true, you can modify your code and set [property]+Specified to true in the set part of [property].
It’s possible to do the opposite, using the deserialize method, you can build an instance of the class from an XML. The main reason for me to write this document is to introduce the xsd.exe tool and the XmlSerializer class which I think can be very useful for simple and may be advanced classes, it doesn’t support serialization of some object types like collections, but if you are a good programmer you’ll find for sure the right “patch” for implementing it. I used it to write a class from a 5000 lines XML schema and it helped a lot.
�
No Comments
No comments yet.
Comments RSS TrackBack Identifier URI
Leave a comment






