Home > Articles > Web Services > XML

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

An Example Program

Example program UndoRedo, shown in Figure 1, uses SOAP (simple object access protocol) serializations to provide undo and redo features. Select a drawing tool from the toolbar. Then click and drag to draw a new object. Click and drag again to create more objects of the same type, or select a new tool.

Figure 1 Program UndoRedo uses SOAP serializations to provide undo and redo features.

After you have drawn something, the program enables the Edit menu's Undo command. Use that command to remove the last thing you drew. Use Undo repeatedly to remove other objects.

After you have undone an object, the program enables the Edit menu's Redo command. Use the Redo command to restore the last object you removed.

The File menu's Save As command lets you save your current drawing in a file. The Open command lets you load a saved picture.


Example program UndoRedo uses a SoapFormatter to save its serializations in a SOAP format. I decided to use the SoapFormatter for two reasons:

  • First, the program saves objects that are subclassed from other objects. These classes confuse the XmlSerializer so that rules out XmlSerializer. That's a shame because XmlSerializer produces a more concise and readable result than SoapFormatter.

  • Second, the other candidate, BinaryFormatter, produces binary serializations that you cannot read. The program could use a binary serialization, but it's easier to debug a program when you can read and modify its output. When there's a problem saving or loading a serialization, you can take a look and see what's going wrong. You can even make changes to the text serialization to see what happens.

Serializations created by the BinaryFormatter take less space than SOAP serializations, so you may prefer the binary serialization when space is at a premium. You will probably be better off debugging your program with the SoapFormatter first and then switching to a BinaryFormatter after everything works. In most cases, the space saving is a minor issue anyway.

Listing 1 shows the SOAP serialization for a small drawing containing a rectangle and an ellipse. I have added indentation to make the result easier to read.

The SOAP-ENC:Array tag represents the program's main serialization object. The SOAP-ENC:ArrayType attribute indicates that this is an array containing three Drawable objects.

The three item tags contained inside the SOAP-ENC:Array tag represent the objects contained in the main Drawable array. The first item's xsi:null attribute indicates that its item is empty. In the program, the first item in the array is not used so the array does contain an empty entry. The second and third items refer to the XML elements named ref-3 and ref-4 that follow in the serialization.

The next a1:DrawableRectangle element represents the first drawing object. Its id attribute indicates that this is the ref-3 object to which the second item element refers. This item has four properties: X1, Y1, X2, and Y2.

The final object in the serialization is an a1:DrawableEllipse element. This is the ref-4 object to which the third item element refers. This item also has four properties: X1, Y1, X2, and Y2.

Listing 1. SOAP Serialization Representing a Rectangle and an Ellipse

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  <SOAP-ENC:Array SOAP-ENC:arrayType="a1:Drawable[3]">
   <item xsi:null="1"/>
   <item href="#ref-3"/>
   <item href="#ref-4"/>
  <a1:DrawableRectangle id="ref-3">
  <a1:DrawableEllipse id="ref-4">

SoapFormatter Namespace

Normally, when you use a class such as SoapFormatter, you use an Imports statement at the beginning of the module to tell Visual Basic to include the class' namespace. Unfortunately, Visual Basic doesn't initially know about the SoapFormatter. To make it available, you need to add a reference to the DLL that defines it.

Right-click on the Solution Explorer's References entry, and select Add Reference. Locate the entry named System.Runtime.Serialization.Formatters.Soap.dll, double-click it, and click the OK button. Now, you can add this Imports statement to include the namespace:

Imports System.Runtime.Serialization.Formatters.Soap

Your program can now create SoapFormatter objects, as in the following code:

Dim soap_formatter As New SoapFormatter()

Drawable Class

The Drawable class and its derivative classes, shown in Listing 2, represent the objects drawn by the UndoRedo program. The Drawable class declares the Draw subroutine with the MustOverride key word, so the derived classes must implement their own versions of this routine. Drawable has four public variables, X1, Y1, X2, and Y2, which define the area the object occupies.

The DrawableEllipse, DrawableRectangle, and DrawableLine classes implement their Draw subroutines in different ways to draw different shapes.

Listing 2. The Drawable Class and its Derivatives Represent Drawing Objects

' Drawable is an object that can draw itself.
<Serializable()> Public MustInherit Class Drawable
  ' Draw the specific shape.
  Public MustOverride Sub Draw(ByVal gr As Graphics)

  ' The bounding box.
  Public X1 As Integer
  Public Y1 As Integer
  Public X2 As Integer
  Public Y2 As Integer
End Class

<Serializable()> Public Class DrawableEllipse
  Inherits Drawable

  ' Draw the shape.
  Public Overrides Sub Draw(ByVal gr As Graphics)
    gr.DrawEllipse(Pens.Black, _
      Math.Min(X1, X2), _
      Math.Min(Y1, Y2), _
      Math.Abs(X2 - X1), _
      Math.Abs(Y2 - Y1))
  End Sub
End Class

<Serializable()> Public Class DrawableRectangle
  Inherits Drawable

  ' Draw the shape.
  Public Overrides Sub Draw(ByVal gr As Graphics)
    gr.DrawRectangle(Pens.Black, _
      Math.Min(X1, X2), _
      Math.Min(Y1, Y2), _
      Math.Abs(X2 - X1), _
      Math.Abs(Y2 - Y1))
  End Sub
End Class

<Serializable()> Public Class DrawableLine
  Inherits Drawable

  ' Draw the shape.
  Public Overrides Sub Draw(ByVal gr As Graphics)
    gr.DrawLine(Pens.Black, X1, Y1, X2, Y2)
  End Sub
End Class

You could extend these classes to provide other drawing features such as fill style, fill color, outline color, drawing style, and so forth. The classes shown in Listing 2 are good enough for this example.


Listing 3 shows the UndoRedo program's code that deals most directly with snapshots. The m_DrawingObjects array holds the current picture's Drawable objects. For easier indexing, entry 0 in the array is allocated but not used.

The m_UndoStack collection holds the program's snapshots. Each entry in the collection is a serialization of the m_DrawingObjects array. The variable m_CurrentSnapshot gives the index in m_UndoStack of the snapshot representing the currently displayed drawing.

The Serialization property makes saving and restoring the program's drawing easy. The Serialization property get procedure returns a SOAP serialization for the m_DrawingObjects array. The Serialization property set procedure takes a serialization and deserializes it to reinitialize the m_DrawingObjects array.

When you select the File menu's Save As command, the mnuFileSaveAs_Click event handler saves the Serialization property into the selected file. Some programs, such as WordPad, empty the undo stack when you save. Other programs, such as Word, leave the undo buffer unchanged. This program takes the second approach.

When you select the File menu's Open command, the mnuFileOpen_Click event handler reads the contents of the file you select into a string. It then sets the program's Serialization property to that string, and the Serialization property set procedure uses the serialization from the file to restore the saved drawing.

The SaveSnapshot subroutine adds a snapshot of the current picture to the m_UndoStack collection. The routine begins by removing any snapshots that come after the current one. If you undo several changes and then draw a new shape, the program calls SaveSnapshot. At that point, the program discards the actions you undid earlier.

Next, SaveSnapshot saves the Serialization value into the m_UndoStack collection. If the stack is too big, the routine removes some of the oldest serializations. This program saves at most 10 serializations, so it is easy for you to see how this works. A real application could probably save far more snapshots. For this program, each Drawable object adds about 125 bytes to the serialization. If a picture has 100 Drawable objects, its serialization takes around 12,500 bytes. Saving 100 serializations of this size in the undo stack would take up about 1.25MB of memory. You could probably afford that much memory, although you might want to stop short of 1,000 serializations, which would take up about 12.5MB.

After shrinking the undo stack, if necessary, the event handler sets m_CurrentSnapshot to the index of the most recent snapshot and calls subroutine EnableUndoMenuItems. That routine enables or disables the Undo and Redo commands as appropriate.

The Undo subroutine decrements m_CurrentSnapshot so it points to the previous snapshot. It sets the Serialization property to that snapshot to restore the picture in its previous state and redraws the picture.

The Redo subroutine increments m_CurrentSnapshot so it points to the next snapshot. It sets the Serialization property to that snapshot and redraws the picture.

Listing 3. The UndoRedo Program Uses this Undo/Redo and File Saving/Opening Code

' The drawing objects.
Private m_DrawingObjects() As Drawable
Private m_MaxDrawingObject As Integer

' The undo stack.
Private m_UndoStack As Collection
Private m_CurrentSnapshot As Integer

' Get ready.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
  ' Code omitted...

  ' Make an empty 1-entry array of drawing objects.
  ReDim m_DrawingObjects(0)
  m_MaxDrawingObject = 0

  ' Make the empty undo stack.
  m_UndoStack = New Collection()
  m_CurrentSnapshot = 0

  ' Save a blank snapshot.
End Sub

' Get or set our serialization.
Private Property Serialization() As String
  ' Return a serialization for the current objects.
    Dim soap_formatter As New SoapFormatter()
    Dim memory_stream As New MemoryStream()

    ' Serialize the m_DrawingObjects.
    soap_formatter.Serialize(memory_stream, m_DrawingObjects)

    ' Rewind the memory stream to the beginning.
    memory_stream.Seek(0, SeekOrigin.Begin)

    ' Return a textual representation.
    Dim stream_reader As New StreamReader(memory_stream)
    Return stream_reader.ReadToEnd()
  End Get

  ' Load objects from the new serialization.
  Set(ByVal Value As String)
    Dim string_reader = New StringReader(Value)
    Dim soap_formatter As New SoapFormatter()

    ' Load the new objects.
    Dim memory_stream As New MemoryStream()
    Dim stream_writer As New StreamWriter(memory_stream)

    ' Write the serialization into the 
    ' StreamWriter and thus the MemoryStream.

    ' Rewind the MemoryStream.
    memory_stream.Seek(0, SeekOrigin.Begin)

    ' Deserialize.
    m_DrawingObjects = soap_formatter.Deserialize(memory_stream)

    ' Save the new objects.
    m_MaxDrawingObject = m_DrawingObjects.GetUpperBound(0)

    ' Display the new objects.
  End Set
End Property

' Open a saved file.
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles mnuFileOpen.Click
  ' Let the user pick a file.
  If dlgOpen().ShowDialog = DialogResult.OK Then
    ' Set our serialization to the file's contents.
      Dim stream_reader As New StreamReader(dlgOpen.FileName)
      Me.Serialization = stream_reader.ReadToEnd()

      ' Remove all snapshots.
      m_CurrentSnapshot = 0

      ' Save a snapshot of the new objects.
    Catch exc As Exception
      MsgBox("Error loading file " & _
        dlgOpen.FileName & vbCrLf & exc.Message)
    End Try
  End If
End Sub

' Save the current drawing objects.
Private Sub mnuFileSaveAs_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles mnuFileSaveAs.Click
  ' Let the user pick a file.
  If dlgSave().ShowDialog = DialogResult.OK Then
    ' Save the objects into the file.
      Dim stream_writer As New StreamWriter(dlgSave.FileName)
    Catch exc As Exception
      MsgBox("Error saving file " & _
        dlgSave.FileName & vbCrLf & exc.Message)
    End Try
  End If

  ' Some programs flush the undo stack at this point.
  ' This program does not.
End Sub

' Save a snapshot.
Private Sub SaveSnapshot()
  Const MAX_SNAPSHOTS = 10

  ' Remove all snapshots items after the current one.
  Do While (m_CurrentSnapshot < m_UndoStack.Count)
    m_UndoStack.Remove(m_CurrentSnapshot + 1)

  ' Save the new snapshot.

  ' If we have too many snapshots, remove the oldest.
  Do While (m_UndoStack.Count > MAX_SNAPSHOTS)

  ' Save the index of the current snapshot.
  m_CurrentSnapshot = m_UndoStack.Count

  ' Enable the proper undo/redo menu items.
End Sub

' Enable the undo and redo menu commands appropriately.
Private Sub EnableUndoMenuItems()
  mnuEditUndo.Enabled = (m_CurrentSnapshot > 1)
  mnuEditRedo.Enabled = (m_CurrentSnapshot < m_UndoStack.Count)
End Sub

' Restore the previous snapshot.
Private Sub Undo()
  ' Do nothing if there are no more snapshots.
  If m_CurrentSnapshot < 2 Then Exit Sub

  ' Restore the previous snapshot.
  m_CurrentSnapshot = m_CurrentSnapshot - 1
  Serialization = m_UndoStack(m_CurrentSnapshot)

  ' Redraw.
End Sub

' Restore the next snapshot.
Private Sub Redo()
  ' Do nothing if there are no more snapshots.
  If m_CurrentSnapshot >= m_UndoStack.Count Then Exit Sub

  ' Restore the next snapshot.
  m_CurrentSnapshot = m_CurrentSnapshot + 1
  Serialization = m_UndoStack(m_CurrentSnapshot)

  ' Redraw.
End Sub

If you look closely at the Serialization property get procedure, you'll see that it doesn't depend on the structure of the m_DrawingObjects array that it is serializing. The procedure tells the SoapFormatter to serialize m_DrawingObjects, and the formatter figures out how to do that. Similarly, the Serialization property set procedure tells the SoapFormatter to re-create m_DrawingObjects from a serialization, and the SoapFormatter figures out how to do it.

This means you do not need to change the serialization code if you change the way the program stores its data. For example, if you change the definition of the Drawable class or its derived classes, or if you create new derived classes, the Serialization procedures will still work. You may need to do some work to load older files using the new class definitions, but the serialization code will run unchanged.

Download the source file here:

Click here to program UndoRedo.
  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020