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:
Delimiting XML tag
Entire set of appointment data
Each appointment in the set
Start time of an appointment
End time of an appointment
Title of an appointment
Location of an appointment
Details of an appointment
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.
Imports System.IO Imports System.Xml Imports Outlook = Microsoft.Office.Interop.Outlook Namespace OutlookAddIn2 Class Sample Function CreateAppointmentsFromXml(ByVal application As Outlook.Application, _ ByVal xml As String) As Outlook.AppointmentItem() Dim appointments As New List(Of Outlook.AppointmentItem) Dim xmlDoc As New XmlDocument() ' If xml is an XML string, create the XML document directly. If xml.StartsWith("<?xml") Then xmlDoc.LoadXml(xml) ElseIf (File.Exists(xml)) Then xmlDoc.Load(xml) Else Throw New Exception("The input string is not valid XML data or the specified file doesn't exist.") End If ' Select all appointment nodes under the root appointments node. Dim appointmentNodes As XmlNodeList = xmlDoc.SelectNodes("appointments/appointment") For Each appointmentNode As XmlNode In appointmentNodes ' Create a new AppointmentItem object. Dim newAppointment As Outlook.AppointmentItem = _ DirectCast(application.CreateItem(Outlook.OlItemType.olAppointmentItem), _ Outlook.AppointmentItem) ' Loop over all child nodes, check the node name, and import the data into the appointment fields. For Each node As XmlNode In appointmentNode.ChildNodes Select Case (node.Name) Case "starttime" newAppointment.Start = DateTime.Parse(node.InnerText) Case "endtime" newAppointment.End = DateTime.Parse(node.InnerText) Case "subject" newAppointment.Subject = node.InnerText Case "location" newAppointment.Location = node.InnerText Case "body" newAppointment.Body = node.InnerText End Select Next ' Save the item in the default calendar. newAppointment.Save() appointments.Add(newAppointment) Next ' Return an array of new appointments. Return appointments.ToArray() End Function End Class End Namespace