共用方式為


將約會 XML 數據匯入 Outlook 約會物件

本主題示範如何讀取以 XML 格式化的約會數據、將數據儲存至預設行事曆中的 Outlook AppointmentItem 物件,以及傳回數位中的約會物件。

範例

注意事項

Helmut Obertanner 提供下列程式代碼範例。 Helmut 的專業知識適用於 Visual Studio 和 Outlook 的 Office 開發人員工具。

下列程式代碼範例包含 Sample 類別的 CreateAppointmentsFromXml 方法,實作為 Outlook 載入宏專案的一部分。 每個項目都會新增以 Microsoft.Office.Interop.Outlook 命名空間為基礎的 Outlook 主要 Interop 元件參考。

CreateAppointmentsFromXml 方法接受兩個輸入參數:

  • 應用程式是受信任的 Outlook 應用程式 物件。

  • xml 是 XML 字串,或代表有效 XML 檔案路徑的字串。 基於下列程式代碼範例的目的,XML 會使用下列 XML 標記來分隔約會數據:

    約會資料

    分隔 XML 標籤

    整組約會資料

    約會

    該組的每一項約會

    委任

    約會的開始時間

    starttime

    約會的結束時間

    endtime

    約會的標題

    主題

    約會的位置

    位置

    約會的詳細資料

    body

下列範例會顯示 xml 參數的輸入資料。

<?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>

CreateAppointmentsFromXml 方法會使用 XML 文件物件模型 (DOM) 的 Microsoft COM 實作來載入及處理 xml 提供的 XML 資料。 CreateAppointmentsFromXml 先檢查 xml 是否指定 XML 資料的有效來源。 如果是,它會將資料載入 XML 文件 DOMDocument。 否則,CreateAppointmentsFromXml 會擲出例外狀況。 如需 XML DOM 的詳細資訊,請參閱 DOM

針對以 XML 數據中的約會卷標分隔的每個約會子節點,CreateAppointmentsFromXml 會尋找特定標記、使用 DOM 來擷取數據,並將數據指派給 AppointmentItem 對象的對應屬性: StartEndSubjectLocationBody。 然後,CreateAppointmentsFromXml 會將約會儲存到預設行事曆。

CreateAppointmentsFromXml 會使用 System.Collections.Generic 命名空間中 List<T> 類別的 Add 方法來匯總這些 AppointmentItem 物件。 當此方法已處理 XML 資料中的所有約會之後,它會以陣列傳回 AppointmentItem 物件。

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#.

Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;

以下是 Visual Basic 程式代碼範例,後面接著 C# 程式代碼範例。

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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookAddIn1
{
    class Sample
    {
        Outlook.AppointmentItem[] CreateAppointmentsFromXml(Outlook.Application application, 
            string xml)
        {
            // Create a list of appointment objects.
            List<Outlook.AppointmentItem> appointments = new 
                List<Microsoft.Office.Interop.Outlook.AppointmentItem>();
            XmlDocument xmlDoc = new XmlDocument();

            // If xml is an XML string, create the document directly. 
            if (xml.StartsWith("<?xml"))
            {
                xmlDoc.LoadXml(xml);
            }
            else if (File.Exists(xml))
            {
                xmlDoc.Load(xml);
            }
            else
            {
                throw new Exception(
                    "The input string is not valid XML data or the specified file doesn't exist.");
            }

            // Select all appointment nodes under the root appointments node.
            XmlNodeList appointmentNodes = xmlDoc.SelectNodes("appointments/appointment");
            foreach (XmlNode appointmentNode in appointmentNodes)
            {

                // Create a new AppointmentItem object.
                Outlook.AppointmentItem newAppointment = 
                    (Outlook.AppointmentItem)application.CreateItem(Outlook.OlItemType.olAppointmentItem);

                // Loop over all child nodes, check the node name, and import the data into the 
                // appointment fields.
                foreach (XmlNode node in appointmentNode.ChildNodes)
                {
                    switch (node.Name)
                    {

                        case "starttime":
                            newAppointment.Start = DateTime.Parse(node.InnerText);
                            break;

                        case "endtime":
                            newAppointment.End = DateTime.Parse(node.InnerText);
                            break;

                        case "subject":
                            newAppointment.Subject = node.InnerText;
                            break;

                        case "location":
                            newAppointment.Location = node.InnerText;
                            break;

                        case "body":
                            newAppointment.Body = node.InnerText;
                            break;

                    }
                }

                // Save the item in the default calendar.
                newAppointment.Save();
                appointments.Add(newAppointment);
            }

            // Return an array of new appointments.
            return appointments.ToArray();
        }

    }
}

另請參閱