File-based Export Only

This topic shows how a file-based connected data source uses connected data source extensions to import and export objects.

The following C# example shows how a file-based connected data source uses a connected data source extension to export objects. For more information about how this extension works, see Example: Connected Data Source Extension for File-Based Data Sources.

using System;

using System.IO;

using System.Xml;

using System.Text;

using System.Collections.Specialized;

using Microsoft.MetadirectoryServices;

namespace SampleMAFileExport

{
    
    public class SampleMAFileExport : 
     
    
        IMAExtensibleFileImport, 
    
        IMAExtensibleFileExport
    {
        //
        // Constructor
        //
        public SampleMAFileExport()
        
        {
            
            m_xmlWriterExport   = null;
            
            m_encoding          = UnicodeEncoding.Unicode;
            
        }
         
        public void GenerateImportFile( 
        
            string                      filename, 
       
            string                      connectTo, 
       
            string                      user, 
       
            string                      password, 
       
            ConfigParameterCollection   configParameters,
       
            bool                        fullImport, 
       
            TypeDescriptionCollection   types,
       
            ref string                  customData 
       
            )
       
        {

            throw new EntryPointNotImplementedException();
                
        }

        //
        //  IMAExtensibleFileExport interface methods
        //

        
        public void DeliverExportFile(
        
            string                      fileName,
        
            string                      connectTo,
        
            string                      user,
        
            string                      password,
       
            ConfigParameterCollection   configParameters,
       
            TypeDescriptionCollection   types
       
            )
        
        {
            
            StreamReader    sr = new StreamReader(
            
                fileName, 
            
                m_encoding
            
                );
                                                 
            string          lineContents = null;
            
            string          exportFile = null;
            
            exportFile = MAUtils.MAFolder.ToString() + @"\sample_export.xml";

            m_xmlWriterExport= new XmlTextWriter(
                                    
                                                    exportFile, 
                                                                        
                                                    m_encoding
                                                                                
                                                );

            m_xmlWriterExport.WriteStartElement(Nodes.Root);

            while (null != (lineContents = sr.ReadLine()))
            
            {
            
                char[]      commaEscape     = new char[] {','};
            
                char[]      quoteEscape     = new char[] {'"'};
            
                string[]    valueComponents = lineContents.Split(commaEscape);
                
                //
                // NOTE: In our sample, we assume that the order given to us is:
                //  objectclass, delta, anchor-attribute, name, email
                //

                //
                // Check the objectclass node and see if this object class is
                // something that we are interested in.
                //
                if (Nodes.ObjectClass == valueComponents[0].Trim(quoteEscape))
            
                {
            
                    continue;
            
                }

                //
                // This means that we are interested in this object class.
                // Populate the comma-delimited file.
                //
                m_xmlWriterExport.WriteStartElement(
                
                                                     Nodes.Object
                                                    
                                                   );

                m_xmlWriterExport.WriteElementString(
                    
                                                      Nodes.ObjectClass,
                                                                                        
                                                      valueComponents[0].Trim(quoteEscape)
                                                        
                                                    );

                m_xmlWriterExport.WriteElementString(
                    
                                                      Nodes.Delta,
                                                    
                                                      valueComponents[1].Trim(quoteEscape)
                                                    
                                                    );

                m_xmlWriterExport.WriteElementString(
                    
                                                     Nodes.Anchor,
                                                    
                                                     valueComponents[2].Trim(quoteEscape)
                                                    
                                                    );

                m_xmlWriterExport.WriteElementString(
                    
                                                     Nodes.Name,
                                                    
                                                     valueComponents[3].Trim(quoteEscape)
                                                    
                                                    );

                m_xmlWriterExport.WriteElementString(
                    
                                                     Nodes.Email, 
                                                    
                                                     valueComponents[4].Trim(quoteEscape)
                                                    
                                                    );

                
                m_xmlWriterExport.WriteEndElement();
                
            }

            m_xmlWriterExport.WriteEndElement();
            
            m_xmlWriterExport.Close();
        }

        //
        // Members
        //
        XmlTextWriter       m_xmlWriterExport;
        
        Encoding            m_encoding;
        
    }
 
    struct Nodes
    {
        //
        // Struct used to keep track of the XML node names.
        // This is used when generating the XML file.
        //

        public const string Root        = "sample-objects";

        public const string Object      = "object";

        public const string Anchor      = "anchor-attribute";

        public const string Delta       = "delta";

        public const string ObjectClass = "objectclass";

        public const string Name        = "name";            

        public const string Email       = "email";            

    }
    
}

The following Visual Basic .NET example shows how a file-based connected data source uses a connected data source extension to export objects. For more information about how this extension works, see Example: Connected Data Source Extension for File-Based Data Sources.

Imports System

Imports System.IO

Imports System.Xml

Imports System.Text

Imports System.Collections.Specialized

Imports Microsoft.MetadirectoryServices

Public Class SampleMAFileExport

    Implements IMAExtensibleFileImport

    Implements IMAExtensibleFileExport

    Public Sub GenerateImportFile(ByVal fileName As String, _
                                  ByVal connectTo As String, _
                                  ByVal user As String, _
                                  ByVal password As String, _
                                  ByVal configParameters As ConfigParameterCollection, _
                                  ByVal fFullImport As Boolean, _
                                  ByVal types As TypeDescriptionCollection, _
                                  ByRef customData As String) _
                                  Implements IMAExtensibleFileImport.GenerateImportFile

        Throw New EntryPointNotImplementedException

    End Sub

    Public Sub DeliverExportFile(ByVal fileName As String, _
                                 ByVal connectTo As String, _
                                 ByVal user As String, _
                                 ByVal password As String, _
                                 ByVal configParameters As ConfigParameterCollection, _
                                 ByVal types As TypeDescriptionCollection) _
                                 Implements IMAExtensibleFileExport.DeliverExportFile

        Dim sr As New StreamReader(fileName, m_encoding)

        Dim lineContents As String = Nothing

        Dim exportFile As String = Nothing

        exportFile = MAUtils.MAFolder.ToString() + "\sample_export.xml"

        m_xmlWriterExport = New XmlTextWriter(exportFile, m_encoding)

        m_xmlWriterExport.WriteStartElement(Nodes.Root)

        While (Not (lineContents = sr.ReadLine()))

            Dim commaEscape() As Char = {","c}

            Dim quoteEscape() As Char = {ControlChars.Quote}

            Dim valueComponents As String() = lineContents.Split(commaEscape)

            '
            ' NOTE: In our sample, we assume that the order given to us is:
            '  objectclass, delta,anchor-attribute, name, e-mail
            '
            '
            ' Check the objectclass node and see if this object class is
            ' something that we are interested in.
            '
            If Nodes.ObjectClass = valueComponents(0).Trim(quoteEscape) Then

                GoTo ContinueWhile1

            End If

            '
            ' This means that we are interested in this object class.
            ' Populate the comma-delimited file.
            '
            m_xmlWriterExport.WriteStartElement(Nodes.Object)

            m_xmlWriterExport.WriteElementString(Nodes.ObjectClass, valueComponents(0).Trim(quoteEscape))

            m_xmlWriterExport.WriteElementString(Nodes.Delta, valueComponents(1).Trim(quoteEscape))

            m_xmlWriterExport.WriteElementString(Nodes.Anchor, valueComponents(2).Trim(quoteEscape))

            m_xmlWriterExport.WriteElementString(Nodes.Name, valueComponents(3).Trim(quoteEscape))

            m_xmlWriterExport.WriteElementString(Nodes.Email, valueComponents(4).Trim(quoteEscape))

            m_xmlWriterExport.WriteEndElement()

ContinueWhile1:

        End While

        m_xmlWriterExport.WriteEndElement()

        m_xmlWriterExport.Close()

    End Sub

    Private m_xmlWriterExport As XmlTextWriter

    Private m_encoding As Encoding

End Class



Module Nodes
    '
    ' Struct used to keep track of the XML node names.
    ' This is used when generating the XML file.
    '
    Public Const Root As String = "sample-objects"

    Public Const [Object] As String = "object"

    Public Const Anchor As String = "anchor-attribute"

    Public Const Delta As String = "delta"

    Public Const ObjectClass As String = "objectclass"

    Public Const Name As String = "name"

    Public Const Email As String = "email"

End Module 'Nodes 

Send comments about this topic to Microsoft

Build date: 2/16/2009