Browse DevX
Sign up for e-mail newsletters from DevX


Customizing XML Serialization in .NET 2.0 Using the IXmlSerializable Interface : Page 2

In .NET 2.0, Microsoft has exposed the methods of the IXmlSerializable interface, giving you explicit control over the XML schema as well as fine-grained control of the XML created when serializing or deserializing your objects at runtime.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Reading and Writing the XML
The ReadXml() method implementation of the Employee class in Listing 1 uses the XmlReader input argument to read the XML and populates the Employee class's local variables with the values read.

Public Sub ReadXml(ByVal reader As XmlReader) _ Implements IXmlSerializable.ReadXml Dim type As XmlNodeType = reader.MoveToContent() If ((type = XmlNodeType.Element) _ And (reader.LocalName = "employee")) Then _firstName = reader("firstName") _lastName = reader("lastName") _address = reader("address") End If End Sub

The WriteXml() method implementation writes an XML representation of the Employee class, including all the local variables, using the various methods of the XmlWriter class.

Public Sub WriteXml(ByVal writer As XmlWriter) _ Implements IXmlSerializable.WriteXml writer.WriteStartElement _ ("employee", "urn:devx-com") writer.WriteAttributeString _ ("firstName", _firstName) writer.WriteAttributeString _ ("lastName", _lastName) writer.WriteAttributeString _ ("address", _address) writer.WriteEndElement() End Sub

The major chunk of the code in Listing 1 resides in the CreateEmployeeSchema() method, which leverages the XML Schema Object Model (SOM) to create an XML schema dynamically and add that to the XmlSchemaSet object. The code is somewhat repetitive; in this case, it creates an XmlSchemaComplexType and then creates XmlSchemaAttribute and XmlSchemaElement instances to hold the content.

Now that you have implemented the Employee class, you can leverage it from a Web service. The EmployeeService class shown below implements a GetEmployee() method that returns an Employee object.

<%@ WebService Language="VB" Class="EmployeeService" %>

Figure 1. Employee Class Definition in WSDL: Inspecting the WSDL shows you the contents of the <employee> element, along with the <employeeRoot> element
Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols <WebService(Namespace := _ "http://tempuri.org/")> _ <WebServiceBinding (ConformsTo:= _ WsiProfiles.BasicProfile1_1)> _ Public Class EmployeeService Inherits System.Web.Services.WebService <WebMethod()> _ Public Function GetEmployee() As Employee Dim emp As New Employee("Thiru", _ "Thangarathinam", "2644 E Remington " & _ "Place, Chandler, AZ") Return emp End Function End Class
The GetEmployee() method is very simple and straightforward. It just creates an instance of the Employee object and returns that object back to the caller. If you inspect the WSDL of the Web service, you will see the definition of the Employee class as shown in Figure 1. The figure shows the XSD schema representation of the Employee class generated by CreateEmployeeSchema().

Figure 2. Output produced by the EmployeeService: The EmployeeService creates and then serializes an Employee object using the WriteXml() method of the IXmlSerializable interface.
If you browse to the Web service URL in a browser and invoke the GetEmployee() method, you'll see the output shown in Figure 2.

Keep It Short
The point to remember here is that using the IXmlSerializable gives you control over how the .NET framework 2.0 serializes your objects. That capability opens up new avenues of opportunities and interesting scenarios that you can use to create sophisticated Web services using .NET Framework 2.0. For example, you would want this type of control when streaming large amounts of data from a Web application. To enable data streaming, you have to turn off response buffering and then chunk the data into discrete blocks demarcated with XML elements. Using IXmlSerializable, you can control the schema for the chunking format and also control the reading and writing of data to the stream with the ReadXml() and WriteXml() methods. Such XML customization is also very useful when you are exposing your Web services to clients from different platforms and you need to be able to have a fine level of control over how the types are serialized.

Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date