How to: Import Appointment XML Data into Outlook Appointment Objects
This topic shows how to read appointment data formatted in XML, save the data to Outlook AppointmentItem objects in the default calendar, and return the appointment objects in an array.
| | Helmut Obertanner provided the following code examples. Helmut's expertise is in Office Developer Tools for Visual Studio and Outlook. Helmut maintains a professional site at X4Uelectronix. |
The following code examples contain the CreateAppointmentsFromXml method of the Sample class, implemented as part of an Outlook add-in project. Each project adds a reference to the Outlook Primary Interop Assembly, which is based on the Microsoft.Office.Interop.Outlook namespace.
The CreateAppointmentsFromXml method accepts two input parameters:
-
application is a trusted Outlook Application object.
-
xml is either an XML string, or a string that represents a path to a valid XML file. For the purpose of the following code examples, the XML delimits appointment data by using the following XML tags:
Appointment data
Delimiting XML tag
Entire set of appointment data
appointments
Each appointment in the set
appointment
Start time of an appointment
starttime
End time of an appointment
endtime
Title of an appointment
subject
Location of an appointment
location
Details of an appointment
body
The following example shows input data for the xml parameter.
<?xml version="1.0" encoding="utf-8" ?>
<appointments>
<appointment>
<starttime>2009-06-01T15:00:00</starttime>
<endtime>2009-06-01T16:15:00</endtime>
<subject>This is a Test-Appointment</subject>
<location>At your Desk</location>
<body>Here is the Bodytext</body>
</appointment>
<appointment>
<starttime>2009-06-01T17:00:00</starttime>
<endtime>2009-06-01T17:15:00</endtime>
<subject>This is a second Test-Appointment</subject>
<location>At your Desk</location>
<body>Here is the Bodytext</body>
</appointment>
<appointment>
<starttime>2009-06-01T17:00:00</starttime>
<endtime>2009-06-01T18:15:00</endtime>
<subject>This is a third Test-Appointment</subject>
<location>At your Desk</location>
<body>Here is the Bodytext</body>
</appointment>
</appointments>
The CreateAppointmentsFromXml method uses the Microsoft COM implementation of the XML Document Object Model (DOM) to load and process the XML data that xml provides. CreateAppointmentsFromXml first checks whether xml specifies a valid source of XML data. If so, it loads the data into an XML document, DOMDocument. Otherwise, CreateAppointmentsFromXml throws an exception. For more information about the XML DOM, see DOM.
For each appointment child node delimited by the appointment tag in the XML data, CreateAppointmentsFromXml looks for specific tags, uses the DOM to extract the data, and assigns the data to corresponding properties of an AppointmentItem object: Start, End, Subject, Location, and Body. CreateAppointmentsFromXml then saves the appointment to the default calendar.
CreateAppointmentsFromXml uses the Add method of the List<T> class in the System.Collections.Generic namespace to aggregate these AppointmentItem objects. When the method has processed all the appointments in the XML data, it returns the AppointmentItem objects in an array.
If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.
The following is the Visual Basic code example, followed by the C# code example.