Instantiating and Initializing an Enhanced Presence Data Class

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.

To generate the XML elements for Enhanced Presence data in a .NET Framework application, you must add the generated classes or their references to the application's project, instantiate desired presence data classes, assign appropriate values to the presence object, and then serialize them into their XML representation. The first three steps are discussed in this topic while the last step is discussed in Serializing an Enhanced Presence Data Object to an XML Element.

Include Generated Classes in Visual Studio Projects

After you have created the C# classes from the Enhanced Presence Schemas (XSD) files, you can incorporate them into your project in one of the following ways.

  • Include the source (.cs) file or files directly in your project.

  • Include the compiled assembly (.dll) containing the Enhanced Presence Data classes in your project and add a reference to it.

To include the source files in a Visual Studio project

  1. Launch Visual Studio.

  2. Click the File menu item, select New Project to create a new project or select Open Project to open an existing one.

  3. In the Solution Explorer, right click the project name, click Add, and then click Existing Item.

  4. In the Add Existing Item dialog box, navigate to the directory containing the generated presence data classes, highlight all the files of interest, and then click Add.

To include the assembly in a Visual Studio project

  1. Launch Visual Studio.

  2. Click the File menu item, select New Project to create a new project or select Open Project to open an existing one.

  3. In the Solution Explorer, right-click the project name, click Add Reference.

  4. In the Add Reference dialog box, select the Browse tab, navigate to the directory containing the assembly compiled from the presence data class source files, and then click the file name.

Instantiate and Initialize the Presence Data Class for Note

A piece of Enhanced Presence note data is represented by a <note> element as defined in the note.xsd file. The structure of the <note> element is prescribed by the complex type named noteType.The XSD.EXE tool translates this type into a C# class of the same name. To generate a <note> element in a C# application, you instantiate the noteType class and assign appropriate values to the newly created noteType instance, and then serialize this initialized noteType object into a <note> element according to the definition of the XSD type named noteType. These steps are illustrated in the following example.

            noteType note = new noteType();
            noteTypeBody body = new noteTypeBody();
            body.Value = "My Note Text2!";
            note.body = new noteTypeBody[] { body };
            body.type = "personal";
            body.uri = "";
            string _noteXml = Serialize(note, typeof(noteType));

In the previous example, Serialize is a method that serializes the note object into a <note> element as prescribed by the noteType in the note.xsd file. The implementation of the Serialize method is discussed in Serializing an Enhanced Presence Data Object to an XML Element. The second parameter of the Serialize method determines what type of XML element is created to hold the data contained in the first parameter.

The output of the previous code example:

<note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://schemas.microsoft.com/2006/09/sip/note">
   <body
      type="personal"
      uri="">My Note Text2!
   </body>
</note>

Instantiate and Initialize the Presence Data Class for Machine State

In the state.xsd file, there is a complex type named machineState, but the XML element representing a machineState instance is <state>. <state> is used because the machineState type is derived from the abstract stateType type. An abstract type in XSD corresponds to an abstract class in the generated C# code and cannot be instantiated.

To generate an XML element for a machine state in a .NET Framework application, you must instantiate the machineState class, assign appropriate values, and then serialize this initialized machineState object into a <state> element that is prescribed by the stateType type.

            uint _onlineAvail = 3500;
            machineState mState = new machineState();
            mState.availability = (uint)_onlineAvail;
            mState.availabilitySpecified = true;
            mState.manual = false;
            string _machineStateXml = Serialize(mState, typeof(stateType));

The stateType must be specified in the second parameter passed into the Serialize method. The output from the previous code example:

<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xsi:type="machineState" 
       xmlns="http://schemas.microsoft.com/2006/09/sip/state">
   <availability>3500</availability>
</state>

If you pass typeof(machineState) as the second parameter into the Serialize method in the previous code example, the output changes:

<machineState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <availability xmlns="http://schemas.microsoft.com/2006/09/sip/state">3500</availability>
</machineState>

Unfortunately, this type of element is not defined by the Enhanced Presence state schema and any publication of this element is ignored.

See Also

Concepts

Creating Enhanced Presence Data Classes from the XML Schemas

Serializing an Enhanced Presence Data Object to an XML Element