Reading and Writing the XML
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) _
Dim type As XmlNodeType = reader.MoveToContent()
If ((type = XmlNodeType.Element) _
And (reader.LocalName = "employee")) Then
_firstName = reader("firstName")
_lastName = reader("lastName")
_address = reader("address")
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) _
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"
<WebService(Namespace := _
<WebServiceBinding (ConformsTo:= _
Public Class EmployeeService
Public Function GetEmployee() As Employee
Dim emp As New Employee("Thiru", _
"Thangarathinam", "2644 E Remington " & _
"Place, Chandler, AZ")
|Figure 1. Employee Class Definition in WSDL: Inspecting the WSDL shows you the contents of the <employee> element, along with the <employeeRoot> element|
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()
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.