In-Depth

Serialize Arrays and ArrayLists to XML

The XmlSerializer class provides a great way to convert objects to XML and back. However, it can be difficult to serialize collections such as Arrays and ArrayLists properly unless you know a few tricks.

Technology Toolbox: XML

The XmlSerializer class provides a great way to convert (serialize) objects to XML and back (deserialize). However, it can be difficult to serialize collections such as Arrays and ArrayLists properly unless you know a few tricks.

This sample application demonstrates how you can add multiple Car class instances into an Array as well as an ArrayList, then serialize them into an XML structure. You can accomplish this serialization process by using special XML serialization attributes such as XmlArray and XmlArrayItem, found in the System.Xml.Serialization namespace. For example, this code demonstrates how you can identify the type within an ArrayList using the XmlArrayItem attribute along with the C# typeof keyword:

[XmlArray("carsArrayList")]
[XmlArrayItem("car",typeof(Car))]
public ArrayList CarsCollection {
   get {
      return _CarsList;
   }
   set {
      _CarsList = value;
   }
}

Here's the output generated by serializing the ArrayList:

<?xml version="1.0" encoding="utf-16"?>
<carsCollection xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <carsArrayList>
      <car>
         <license>1234</license>
         <color>Black</color>
      </car>
      <car>
         <license>4321</license>
         <color>Blue</color>
      </car>
   </carsArrayList>
</carsCollection>

Download the sample here. To view a live example, visit the XML for ASP.NET Developers Web site.

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube