All formats

FB2Library

LanguageC# (.NET Standard 1.0: .NET Framework 4.5+, .NET Core, .NET 6+, Mono, Xamarin)
LicenseMicrosoft Public License (MS-PL)
Repositorygithub.com/wcoder/FB2Library
Documentationwiki
Depends onnothing outside .NET
Read / writeyes / yes (serializes the model back to XML)

FB2Library parses an FB2 file into FB2File, an object model of the FictionBook schema: TitleInfo (title, authors, genres, annotation, language, cover, sequences), DocumentInfo, PublishInfo, the Bodies with their SectionItem tree, and the Images dictionary of decoded binaries. FB2File.ToXML() turns the model back into an XDocument. The loadHeaderOnly flag reads only the description, which is fast for cataloging large collections.

The library is small and stable; it’s a port of the older fb2librarynet project to .NET Standard. It has seen few changes in recent years, so for new .NET code that needs editing APIs consider Fb2.Document.

Installation#

Shell
dotnet add package FB2Library

Basic usage: read metadata and chapters#

C#
using System.Xml;
using FB2Library;
using FB2Library.Elements;

var settings = new XmlLoadSettings(new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore });
FB2File file;
using (var stream = File.OpenRead("book.fb2"))
{
    file = await new FB2Reader().ReadAsync(stream, settings);
}

var info = file.TitleInfo;
Console.WriteLine("Title:    " + info.BookTitle?.Text);
foreach (var author in info.BookAuthors)
    Console.WriteLine($"Author:   {author.FirstName?.Text} {author.LastName?.Text}");
Console.WriteLine("Language: " + info.Language);

void PrintSection(SectionItem section, int level)
{
    Console.WriteLine(new string(' ', level * 2) + section.Title?.ToString().Trim());
    foreach (var sub in section.SubSections)
        PrintSection(sub, level + 1);
}
foreach (var section in file.MainBody.Sections)
    PrintSection(section, 0);

How to extract the text of a chapter#

SectionItem.Content holds paragraphs, poems, citations and other items; ToString() returns their text:

C#
SectionItem chapter = file.MainBody.Sections[0];
foreach (IFb2TextItem item in chapter.Content)
{
    if (item is ParagraphItem p)
        Console.WriteLine(p.ToString());
}

How to extract images from an FB2 file#

Images maps each binary id to its decoded bytes:

C#
Directory.CreateDirectory("images");
foreach (var (id, binary) in file.Images)
{
    File.WriteAllBytes(Path.Combine("images", id), binary.BinaryData);
    Console.WriteLine($"{id}: {binary.ContentType}, {binary.BinaryData.Length} bytes");
}

How to read only the metadata of many files#

Load the XML yourself and pass loadHeaderOnly: true to skip the body and images:

C#
using System.Xml.Linq;

foreach (var path in Directory.EnumerateFiles("library", "*.fb2"))
{
    var file = new FB2File();
    file.Load(XDocument.Load(path), true);
    Console.WriteLine($"{file.TitleInfo.BookTitle?.Text} ({path})");
}

How to save an FB2 file#

C#
XDocument xml = file.ToXML(false);  // true: export only the description
xml.Save("out.fb2");

Tips#