Iterate through container items

GroupDocs.Parser provides the functionality to extract items from containers by the GetContainer method:

IEnumerable<ContainerItem> GetContainer();

This method returns a collection of ContainerItem objects:

MemberDescription
NameThe name of the item.
DirectoryThe directory of the item.
FilePathThe full path of the item.
SizeThe size of the item in bytes.
MetadataThe collection of item metadata.
DetectFileTypeDetects a file type of the container item.
Stream OpenStream()Opens the stream of the item content.
Parser OpenParser()Creates the Parser object for the item content.
Parser OpenParser(LoadOptions)Creates the Parser object for the item content with LoadOptions.
Parser OpenParser(LoadOptions, ParserSettings)Creates the Parser object for the item content with LoadOptions and ParserSettings.

Here are the steps to extract container from the document:

  • Instantiate Parser object for the initial document;
  • Call GetContainer method and obtain collection of ContainerItem objects;
  • Check if collection isn’t null (container extraction is supported for the document);
  • Iterate through the collection and get container item names, sizes and obtain content.

The following example shows how to extract attachments from a container:

// Create an instance of Parser class
using(Parser parser = new Parser(filePath))
{
    // Extract attachments from the container
    IEnumerable<ContainerItem> attachments = parser.GetContainer();
    // Check if container extraction is supported
    if(attachments == null)
    {
        Console.WriteLine("Container extraction isn't supported");
    }

    // Iterate over attachments
    foreach(ContainerItem item in attachments)
    {
        // Print an item name and size
        Console.WriteLine(string.Format("{0}: {1}", item.Name, item.Size));
    }
}

Container represents both container-only files (like zip archives, outlook storage) and documents with attachments (like emails, PDF Portfolios).

In case of outlook storage (ost/pst files) container consists of email documents (msg files)

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document parser App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.