Adding documents to a document base

How to populate a document base (when there is no versioning).

Your program must be connected to a TEXTML Server instance.

Your program must be connected to a docbase on that server, and your program must have an IxiaDocBaseServices object for that docbase:
IxiaDocBaseServices docbase =   // ...

This section explains how to add documents to a document base with no versioning.

Versioning means that a docbase can store more than one version of a document: each version has the same document name, but a unique version number. No versioning means that a new version of a document replaces the existing document.

In the code snippets below, the documents to be added are files located in a path stored in String sourceDirectory.

To add documents to a document base:

  1. Get the DocumentServices object for the specified docbase:
    IxiaDocumentServices ds = docbase.DocumentServices();

    DocumentServices allow you get (retrieve) documents from a docbase, to set (add) documents, to copy documents from one collection to another, to lock documents, and to remove (delete) documents.

  2. Create a document list, i.e., an array of documents to be added to the docbase.
    Each document is an IxiaDocument object that stores the contents of one file in sourceDirectory. The code snippet below shows how to create a document list that contains one document for each file contained in sourceDirectory and in all of its subdirectories (if any).
    // Create an array of File objects, one for each file/directory
    // stored in the source directory
    String[] files = System.IO.Directory.GetFiles(SourcePath);
    
    // Create an ArrayList structure.
    // Eventually, there will be one element for
    // each file/directory
    ArrayList documents = new ArrayList(0);
    
    // For each file/directory...
    for (int i = 0; i < files.Length; ++i)
    {
        // If it is a directory, and user wants recursion, 
        // then process the (child) directory
        if (File.GetAttributes(files[i]) == FileAttributes.Directory)
        {
            if (recurse)
                Process(files[i], ds, true);
        }
        else
        {
            // It is a file. Create a Document object for it.
            IxiaDocument document = IxiaDocument.getInstance();
            document.Name = System.IO.Path.GetFileName(files[i]);
    
            // Let's assume that all files in the directory are
            // XML files.
            document.MimeType = "text/xml";
            document.Content = IxiaDocument.MakeContentFromFile
                (new System.IO.FileStream(files[i],FileMode.Open));
    
            // Add the document to the ArrayList of documents
            documents.Add(document);
        }
    }
    
    // Create <docList>,
    // an array of documents of the same size as the ArrayList
    IxiaDocument[] docList = new IxiaDocument[documents.Count];
    documents.CopyTo(docList);
    
  3. Add the documents in the document list to docbase ds:
    // Add the documents to the docbase.
    // Index the documents.
    // If a document with the same name already exists in the docbase,
    // then replace the old document with the new one.
    // The documents are all of type "user document" (i.e., they are
    // not "system documents").
    
    IxiaTextmlServerError[] err =
        ds.SetDocuments(docList,
                        TextmlConstants.TEXTML_ADD_DOCUMENT |
                        TextmlConstants.TEXTML_REPLACE_DOCUMENT |
                        TextmlConstants.TEXTML_INDEX_DOCUMENT,
                        TextmlDocumentType.TextmlDocument);
    
  4. Handle any errors that may have occurred while adding the list of documents to the docbase.
    // Process the errors, if any
    int countError = 0;
    if (err != null)
    {
        for (int i = 0; i < err.Length; ++i)
        {
            if (err[i] != null)
            {
                ++countError;
                if (countError == 1)
                {
                   // <Message> is an XML string.
                   Console.WriteLine("Error = " + err[i].Message);
                }
            }
        }
    }
    
    Console.WriteLine("End of adding the documents.");
    Console.WriteLine("Error count = " + countError);
    

    Adding a list of documents to a docbase may trigger an error for none, some, or all of the documents. You can handle these errors only after IxiaDocumentServices.SetDocuments() returns. You handle them by processing the array of IxiaTextmlServerError objects. The messages returned by the Error objects are in XML.

You can build and run sample program SetDocuments:

ProgramFiles\IxiaSoft\TextmlServer41\...\SetDocuments.cs