Export (0) Print
Expand All

Package.LoadFromXML Method (String, IDTSEvents)

Loads a package and all its objects that have been saved to memory in XML format. To load a package that is saved to the hard drive, use the Application.LoadPackage method.

Namespace:  Microsoft.SqlServer.Dts.Runtime
Assembly:  Microsoft.SqlServer.ManagedDTS (in Microsoft.SqlServer.ManagedDTS.dll)

public void LoadFromXML(
	string packageXml,
	IDTSEvents events
)

Parameters

packageXml
Type: String
A String that contains the package in XML format.
events
Type: Microsoft.SqlServer.Dts.Runtime.IDTSEvents
An object that implements the IDTSEvents interface for firing events (errors, warnings, and so on) during persistence.

You should use the LoadFromXML method to load the XML that is in memory. If you want to load a package that has been saved to the hard drive as XML, use the Application.LoadPackage method. If the package has been saved to the File System or MSDB database, use the Application.LoadFromSqlServer or Application.LoadFromDtsServer methods.

When you call any of the loading methods on the Application, the runtime will iterate through the tasks, connection managers, log providers, and all other objects that are contained by the package and call the LoadFromXML method on each of them. The contained objects have code in their LoadFromXML that parses the XmlElement for each property that the object must recreate, and the value saved for the element. Therefore, you do not directly call the LoadFromXML on the individual objects, but call the method on the Application object, and the runtime will cascade through the package and call the LoadFromXML on the objects for you.

The following code example creates a package and adds two tasks to it. The package XML is saved to an XmlDocument in memory. A second package is created, and because it is loaded with the same XmlDocument, it is now a duplicate of the first package.

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

namespace Microsoft.SqlServer.SSIS.Samples
{
    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);

            // 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);

            // Reload the package from its XML.
            Package pkg2 = new Package();
            pkg2.LoadFromXML(myPkgDocument, null);
            Console.WriteLine("This is the package XML that pkg2 now contains:");
            Console.Out.WriteLine(myPkgDocument.OuterXml);
        }
    }
}
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft