Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Serializing an Enhanced Presence Data Object to an XML Element

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following implementation serializes a .NET Framework class into an XML string.

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Globalization;

/// <summary>
/// This method wraps an XmlSerializer and turns a presence data class 
/// object into its corresponding XML string.
/// </summary>
/// <param name="objectToSerialize">
/// A presence data class object to be serialized into the string
/// representation of an XML element according to the definition of
/// the type specified in the second parameter of this method. 
/// </param>
/// <param name="type">The type of object to serialize. Depending 
/// on the XML Schema definition, this may have to be a super type 
/// of the object specified in the first parameter</param>
/// <returns>An XML string of the specified presence data</returns>
public static String Serialize(Object objectToSerialize, Type type)
     if (objectToSerialize == null) 
        throw new ArgumentNullException("objectToSerialize");
     if (type == null) 
        throw new ArgumentNullException("type");

     XmlSerializer serializer = new XmlSerializer(type);
     StringWriter sw = new StringWriter(new StringBuilder(128), 
     XmlWriterSettings setting = new XmlWriterSettings();
     setting.OmitXmlDeclaration = true;
     setting.Indent = true;
     setting.IndentChars = "   ";
     setting.NewLineOnAttributes = true;
     XmlWriter messageWriter = XmlWriter.Create(sw, setting);
     serializer.Serialize(messageWriter, objectToSerialize);

     return sw.ToString();
© 2015 Microsoft