Universal.Common.Formats.Dtd 1.0.2

dotnet add package Universal.Common.Formats.Dtd --version 1.0.2
                    
NuGet\Install-Package Universal.Common.Formats.Dtd -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Universal.Common.Formats.Dtd" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Universal.Common.Formats.Dtd" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Universal.Common.Formats.Dtd" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Universal.Common.Formats.Dtd --version 1.0.2
                    
#r "nuget: Universal.Common.Formats.Dtd, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Universal.Common.Formats.Dtd@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Universal.Common.Formats.Dtd&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Universal.Common.Formats.Dtd&version=1.0.2
                    
Install as a Cake Tool

Universal.Common.Formats.Dtd

DTD (Document Type Definition) parser for .NET, providing a complete object model for parsing and working with XML DTD files.

⚠️ Security Notice

This library should only be used with trusted DTD files. The parser does not implement protection against:

  • Billion laughs attacks (entity expansion attacks)
  • Quadratic blowup attacks
  • Excessive entity nesting
  • Other malicious DTD constructs

Do not use this library to parse DTDs from untrusted sources without implementing additional security controls.

Classes

Document

Represents a complete DTD document, including both internal and external subsets.

var document = new Document
{
    SystemId = "http://example.com/mydtd.dtd",
    PublicId = "-//Example//DTD Example 1.0//EN",
    Declarations = new Declaration[] { /* ... */ }
};

DocumentReader

Parses DTD files from various sources including URIs, streams, and strings. Supports parameter entity expansion and external entity resolution.

// Parse from URI
using (var reader = new DocumentReader())
{
    Document doc = await reader.ReadAsync(new Uri("file:///path/to/file.dtd"));
}

// Parse from stream
using (var stream = File.OpenRead("file.dtd"))
using (var reader = new DocumentReader())
{
    Document doc = await reader.ReadAsync(stream);
}

// Parse from string
using (var reader = new DocumentReader())
{
    Document doc = reader.Parse(dtdContent);
}

// Configure options
var options = new DocumentReader.Options
{
    ExpandParameterEntities = true,
    ExternalParameterEntityResolver = new ExternalParameterEntityResolver()
};
using (var reader = new DocumentReader(options))
{
    Document doc = await reader.ReadAsync(uri);
}

Declaration Types

The library supports all standard DTD declaration types:

ElementDeclaration

Defines element structure and content models.

var element = new ElementDeclaration
{
    Name = "book",
    ContentSpecification = new ElementContentSpecification
    {
        Particle = new SequenceGroupContentParticle
        {
            Particles = new ContentParticle[]
            {
                new ElementReferenceContentParticle { Name = "title" },
                new ElementReferenceContentParticle { Name = "author", Occurrence = OccurrenceIndicator.OneOrMore }
            }
        }
    }
};
AttributeListDeclaration

Defines attributes for elements.

var attlist = new AttributeListDeclaration
{
    ElementName = "book",
    Attributes = new[]
    {
        new AttributeDefinition
        {
            Name = "isbn",
            Type = new CDataAttributeType(),
            Default = new RequiredDefault()
        },
        new AttributeDefinition
        {
            Name = "format",
            Type = new EnumerationAttributeType { Values = new[] { "hardcover", "paperback", "ebook" } },
            Default = new DefaultValueDefault { Value = "paperback" }
        }
    }
};
EntityDeclaration

Defines general and parameter entities.

// Internal entity
var internalEntity = new EntityDeclaration
{
    Name = "copyright",
    Definition = new InternalEntity { Value = "Copyright © 2024" }
};

// External entity
var externalEntity = new EntityDeclaration
{
    Name = "chapter1",
    Definition = new ExternalEntity
    {
        SystemId = "chapter1.xml",
        PublicId = "-//Example//TEXT Chapter 1//EN"
    }
};

// Parameter entity
var parameterEntity = new EntityDeclaration
{
    Name = "commonAttrs",
    IsParameterEntity = true,
    Definition = new InternalEntity { Value = "id ID #IMPLIED" }
};
NotationDeclaration

Declares notations for external data formats.

var notation = new NotationDeclaration
{
    Name = "GIF",
    Definition = new SystemNotation { SystemId = "gif-viewer.exe" }
};
ConditionalSection

Supports INCLUDE and IGNORE conditional sections.

var conditional = new ConditionalSection
{
    Type = ConditionalSectionType.Include,
    Declarations = new Declaration[] { /* ... */ }
};
CommentDeclaration

Represents comments in the DTD.

var comment = new CommentDeclaration
{
    Text = "This is a comment"
};
ProcessingInstructionDeclaration

Represents processing instructions.

var pi = new ProcessingInstructionDeclaration
{
    Target = "xml-stylesheet",
    Data = "type=\"text/xsl\" href=\"style.xsl\""
};

Content Specifications

DTD content models are represented by various specification types:

  • EmptyContentSpecification: Element must be empty
  • AnyContentSpecification: Element can contain any content
  • ElementContentSpecification: Element contains structured child elements
  • MixedContentSpecification: Element can contain text and/or child elements
// EMPTY
new EmptyContentSpecification()

// ANY
new AnyContentSpecification()

// Element content: (title, author+)
new ElementContentSpecification
{
    Particle = new SequenceGroupContentParticle
    {
        Particles = new ContentParticle[]
        {
            new ElementReferenceContentParticle { Name = "title" },
            new ElementReferenceContentParticle 
            { 
                Name = "author", 
                Occurrence = OccurrenceIndicator.OneOrMore 
            }
        }
    }
}

// Mixed content: (#PCDATA | emphasis | strong)*
new MixedContentSpecification
{
    Elements = new[] { "emphasis", "strong" }
}

Attribute Types

All XML attribute types are supported:

  • CDataAttributeType: Character data
  • IdAttributeType: Unique identifier
  • IdReferenceAttributeType: Reference to an ID
  • IdReferencesAttributeType: Space-separated list of ID references
  • EntityAttributeType: Reference to an entity
  • EntitiesAttributeType: Space-separated list of entity references
  • NameTokenAttributeType: Name token
  • NameTokensAttributeType: Space-separated list of name tokens
  • EnumerationAttributeType: Enumerated values
  • NotationAttributeType: Notation name

Attribute Defaults

Four types of attribute defaults are supported:

  • DefaultValueDefault: Provides a default value
  • RequiredDefault: Attribute is required (#REQUIRED)
  • ImpliedDefault: Attribute is optional (#IMPLIED)
  • FixedDefault: Attribute has a fixed value (#FIXED)

ExternalParameterEntityResolver

Resolves external parameter entities with configurable security options.

var options = new ExternalParameterEntityResolver.Options
{
    AllowHttp = false,      // Disallow HTTP for security
    AllowHttps = true,      // Allow HTTPS
    AllowFile = true,       // Allow local files
    AllowedDomains = new HashSet<string> { "example.com", "trusted.org" },
    EnableCaching = true    // Cache resolved entities
};

using (var resolver = new ExternalParameterEntityResolver(options))
{
    var readerOptions = new DocumentReader.Options
    {
        ExpandParameterEntities = true,
        ExternalParameterEntityResolver = resolver
    };
    
    using (var reader = new DocumentReader(readerOptions))
    {
        Document doc = await reader.ReadAsync(uri);
    }
}

OccurrenceIndicator

Defines occurrence indicators for content particles:

  • Once: Element appears exactly once (no indicator)
  • Optional: Element appears zero or one time (?)
  • ZeroOrMore: Element appears zero or more times (*)
  • OneOrMore: Element appears one or more times (+)

Usage Examples

Parsing a DTD with DOCTYPE declaration

string dtd = @"
<!DOCTYPE book SYSTEM 'book.dtd' [
    <!ELEMENT book (title, author+)>
    <!ATTLIST book isbn CDATA #REQUIRED>
]>";

using (var reader = new DocumentReader())
{
    Document doc = reader.Parse(dtd);
    
    foreach (var decl in doc.Declarations)
    {
        if (decl is ElementDeclaration elem)
        {
            Console.WriteLine($"Element: {elem.Name}");
        }
    }
}

Parsing an external DTD subset

using (var reader = new DocumentReader())
{
    Document doc = await reader.ReadAsync(
        new Uri("https://example.com/dtds/mydtd.dtd"));
    
    // Process declarations
    var elements = doc.Declarations.OfType<ElementDeclaration>();
    var attributes = doc.Declarations.OfType<AttributeListDeclaration>();
}

Working with parameter entities

var options = new DocumentReader.Options
{
    ExpandParameterEntities = true,
    ExternalParameterEntityResolver = new ExternalParameterEntityResolver(
        new ExternalParameterEntityResolver.Options
        {
            AllowHttps = true,
            AllowFile = true
        })
};

using (var reader = new DocumentReader(options))
{
    Document doc = await reader.ReadAsync(uri);
    // Parameter entities are automatically expanded
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 378 11/21/2025
1.0.1 372 11/21/2025
1.0.0 404 11/21/2025

Added string representations of certain polymorphic types.