Package.SaveToXML(String, IDTSEvents) 方法

定義

將封裝以 XML 格式儲存至記憶體。 若要將封裝以 .xml 儲存至硬碟,請使用 SaveToXml(String, Package, IDTSEvents) 方法。

public:
 void SaveToXML([Runtime::InteropServices::Out] System::String ^ % packageXml, Microsoft::SqlServer::Dts::Runtime::IDTSEvents ^ events);
public void SaveToXML (out string packageXml, Microsoft.SqlServer.Dts.Runtime.IDTSEvents events);
override this.SaveToXML : string * Microsoft.SqlServer.Dts.Runtime.IDTSEvents -> unit
Public Sub SaveToXML (ByRef packageXml As String, events As IDTSEvents)

參數

packageXml
String

Out 參數,其中包含儲存封裝時所建立的 XML。

events
IDTSEvents

實作事件的物件,以引發錯誤、警告或參考事件。

範例

下列程式碼範例會建立封裝,並設定數個封裝屬性。 然後使用方法將封裝儲存到記憶體中 XmlDocument SaveToXML 。 接下來,會將新的工作新增至套件。 程式碼範例的最後一項工作是使用,將整個 XML 儲存至硬碟上的檔案 SaveToXml

using System;  
using System.Collections.Generic;  
using System.Text;    
using Microsoft.SqlServer.Dts.Runtime;  
using Microsoft.SqlServer.Dts.Tasks.BulkInsertTask;  
using System.Xml;  

namespace SaveToXML_API  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Application app = new Application();  

            // Location and file name can be combined into one string,  
            // or location could be set by a variable instead of hard-coded.  
            String XmlLocation = @"C:\XML";  
            String XmlFileName = "TestXML.xml";  
            String XmlFile = XmlLocation + XmlFileName;  
            Package pkg = new Package();  
            pkg.CreatorName = "Test";  
            pkg.Name = "SaveToXML Package";  
            pkg.CheckSignatureOnLoad = true;  
            pkg.DelayValidation = false;  
            pkg.SaveCheckpoints = false;  

            // Create package XmlDocument and use in pkg.SaveToXml.  
            XmlDocument myPkgDocument = new XmlDocument();  
            pkg.SaveToXML(ref myPkgDocument, null, null);  
            // If you want to see what the package XML contains   
            // at this point, uncomment this line and view the console.  
            // Console.Out.WriteLine(myPkgDocument.OuterXml);  

            // Now modify the package. Create a task   
            // and set some properties.  
            Executable execBI = pkg.Executables.Add("STOCK:BulkInsertTask");  
            TaskHost th = execBI as TaskHost;  
            th.Properties["DebugMode"].SetValue(th, false);  
            // You can cast the task here.  
            // BulkInsertTask myTask = th.InnerObject as BulkInsertTask;  

            // Save the task into the package using pkg.SaveToXML.  
            // This saves the package after it has been modified  
            // by the addition of the task.  
            pkg.SaveToXML(ref myPkgDocument, null, null);  

            // When you want to save the package to the hard-drive,  
            // Save using the Application.SaveToXML method.  
            app.SaveToXml(XmlFile, pkg, null);  

        }  
    }  
}  
Imports System  
Imports System.Collections.Generic  
Imports System.Text    
Imports Microsoft.SqlServer.Dts.Runtime  
Imports Microsoft.SqlServer.Dts.Tasks.BulkInsertTask  
Imports System.Xml  

Namespace SaveToXML_API  
    Class Program  
        Shared  Sub Main(ByVal args() As String)  
            Dim app As Application =  New Application()   

            ' Location and file name can be combined into one string,  
            ' or location could be set by a variable instead of hard-coded.  
            Dim XmlLocation As String =  "C:\XML"   
            Dim XmlFileName As String =  "TestXML.xml"   
            Dim XmlFile As String =  XmlLocation + XmlFileName   
            Dim pkg As Package =  New Package()   
            pkg.CreatorName = "Test"  
            pkg.Name = "SaveToXML Package"  
            pkg.CheckSignatureOnLoad = True  
            pkg.DelayValidation = False  
            pkg.SaveCheckpoints = False  

            ' Create package XmlDocument and use in pkg.SaveToXml.  
            Dim myPkgDocument As XmlDocument =  New XmlDocument()   
            pkg.SaveToXML( myPkgDocument,Nothing,Nothing)  
            ' If you want to see what the package XML contains   
            ' at this point, uncomment this line and view the console.  
            ' Console.Out.WriteLine(myPkgDocument.OuterXml);  

            ' Now modify the package. Create a task   
            ' and set some properties.  
            Dim execBI As Executable =  pkg.Executables.Add("STOCK:BulkInsertTask")   
            Dim th As TaskHost =  execBI as TaskHost   
            th.Properties("DebugMode").SetValue(th, False)  
            ' You can cast the task here.  
            ' BulkInsertTask myTask = th.InnerObject as BulkInsertTask;  

            ' Save the task into the package using pkg.SaveToXML.  
            ' This saves the package after it has been modified  
            ' by the addition of the task.  
            pkg.SaveToXML( myPkgDocument,Nothing,Nothing)  

            ' When you want to save the package to the hard-drive,  
            ' Save using the Application.SaveToXML method.  
            app.SaveToXml(XmlFile, pkg, Nothing)  

        End Sub  
    End Class  
End Namespace  

備註

如果您想要將套件以 XML 形式儲存至硬碟,請使用 Microsoft.SqlServer.Dts.Runtime.Application.SaveToXml 方法。 如果您想要將封裝儲存至檔案系統,請使用 Microsoft.SqlServer.Dts.Runtime.Application.SaveToDtsServer 。 如果您想要將封裝儲存到 MSDB 資料庫,請使用 Microsoft.SqlServer.Dts.Runtime.Application.SaveToSqlServerMicrosoft.SqlServer.Dts.Runtime.Application.SaveToSqlServerAs 方法。 當您在 Microsoft.SqlServer.Dts.Runtime.Application.SaveToXml 上呼叫方法時 Application ,執行時間會逐一查看工作、連接管理員、記錄提供者,以及封裝所包含的所有其他物件,並 SaveToXML 在每個物件上呼叫方法。 包含的物件具有中的 SaveToXML 程式碼,其會針對物件必須儲存的每個屬性建立 XmlElement,並為元素建立一個值。 封裝包含了內容,而且物件會將其特定元素附加至封裝的內容中。 因此,您不會直接 SaveToXML 在個別物件上呼叫,而是在物件上呼叫方法 Application ,而執行時間會透過封裝物件來重迭,並 SaveToXML 為您呼叫。

適用於