Serializing an Enhanced Presence Data Object to an XML Element
The following example 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),
CultureInfo.InvariantCulture);
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);
messageWriter.Close();
sw.Close();
return sw.ToString();
}
See Also
Concepts
Creating Enhanced Presence Data Classes from the XML Schemas
Instantiating and Initializing an Enhanced Presence Data Class