API Documentation
AngleSharp.Xml can be used in two complementary ways:
- Integrated in an AngleSharp browsing context via configuration
- Directly through the dedicated XmlParser type
Core entry points
Configuration extension
Use this for content loading and auto-selection of XML / SVG document types.
var config = Configuration.Default
.WithXml();
The extension registers XML and SVG document factories and provides an IXmlParser service.
XmlParser
Use this for direct parsing of strings or streams.
var parser = new XmlParser();
var document = parser.ParseDocument("<book><title>AngleSharp</title></book>");
Async methods are available for both strings and streams:
var document = await parser.ParseDocumentAsync(xmlText);
Parsing fragments
Use ParseFragment when you need to parse child nodes relative to a context element.
var context = document.DocumentElement;
var nodes = parser.ParseFragment("<chapter>Intro</chapter>", context);
XmlParserOptions
XmlParserOptions controls parser behavior.
IsSuppressingErrors
If true, the parser tries to continue instead of throwing parse exceptions.
var parser = new XmlParser(new XmlParserOptions
{
IsSuppressingErrors = true,
});
IsKeepingSourceReferences
If true, elements keep token source references. This is useful for diagnostics and tooling.
MaxCharactersFromEntities
The maximum number of characters that expanding the entities declared in the document's DTD may produce in total, like System.Xml.XmlReaderSettings.MaxCharactersFromEntities. Zero (0) means no limit. Unlike System.Xml, which prohibits DTD processing by default, this parser always processes the DTD, so the limit defaults to 10,000,000 characters (XmlParserOptions.DefaultMaxCharactersFromEntities) to stop entities that expand exponentially ("billion laughs") from exhausting memory.
Exceeding the limit throws an XmlParseException. With IsSuppressingErrors, the references beyond the limit are kept as text instead. The built-in entities (&, <, ...) and character references do not count.
var parser = new XmlParser(new XmlParserOptions
{
MaxCharactersFromEntities = 100_000,
});
MaximumDepth
The deepest level at which the parser nests elements, counting the root element as level 1, like HtmlParserOptions.MaximumDepth in AngleSharp. It defaults to 512 (XmlParserOptions.DefaultMaximumDepth); zero or a negative value restores the default, and Int32.MaxValue lifts the limit. XML puts no limit on nesting, but code walking a tree that is thousands of levels deep recursively can overflow the call stack, which ends the process.
An element beyond the limit throws an XmlParseException. With IsSuppressingErrors, it is attached at the limit instead, as a sibling of the deepest element, as the HTML parser does; text that follows a closed sibling then stays in its own element.
var parser = new XmlParser(new XmlParserOptions
{
MaximumDepth = 2000,
});
OnCreated
Callback invoked when an element has been created, including source position. References to DTD-declared entities are only resolved at the end of the parse, so the element's attribute values still hold them as written.
var parser = new XmlParser(new XmlParserOptions
{
OnCreated = (element, position) =>
{
Console.WriteLine($"{element.NodeName} at {position.Line}:{position.Column}");
}
});
Serialization APIs
ToXml
Serializes with XmlMarkupFormatter:
var xml = document.ToXml();
ToMarkup
Auto-selects formatter (XML / XHTML / HTML) based on document type:
var markup = document.ToMarkup();
XmlMarkupFormatter
You can customize serialization behavior:
var formatter = new XmlMarkupFormatter
{
IsAlwaysSelfClosing = true,
};
var xml = document.ToHtml(formatter);
Canonical XML
ToCanonicalXml produces canonical UTF-8 bytes without a byte-order mark. Canonical XML 1.1 is the default mode.
var canonicalBytes = document.ToCanonicalXml();
Select Exclusive XML Canonicalization 1.0 and its inclusive namespace prefixes through options:
var options = new XmlCanonicalizationOptions
{
Mode = XmlCanonicalizationMode.ExclusiveXml10,
IncludeComments = true,
InclusiveNamespacePrefixes = new[] { "ds", "#default" },
};
document.ToCanonicalXml(outputStream, options);
The stream overload leaves the destination stream open. Both modes remove XML declarations and doctypes, expand empty elements, replace CDATA boundaries with character content, normalize escaping, and order namespace declarations and attributes canonically.
Canonicalization accepts complete documents and rooted element subtrees. Canonical XML 1.1 subtree output carries applicable ancestor namespace, xml:lang, xml:space, and fixed-up xml:base context. Exclusive mode emits visibly used namespaces plus any configured inclusive prefixes.
XSD validation
Validate an existing document against one or more inline XML Schema 1.0 documents:
var result = document.ValidateXsd(commonSchema, documentSchema);
if (!result.IsValid)
{
foreach (var diagnostic in result.Diagnostics)
{
Console.WriteLine($"{diagnostic.Severity}: {diagnostic.Message} ({diagnostic.LineNumber}:{diagnostic.LinePosition})");
}
}
Validation collects errors and warnings by default. Use options to stop after the first error or suppress warnings:
var options = new XsdValidationOptions
{
IsFailFast = true,
IsReportingWarnings = false,
};
var result = document.ValidateXsd(schemas, options);
For schemas using xs:include or xs:import with locations, configure a System.Xml.Schema.XmlSchemaSet with source URIs and a resolver, then pass it to ValidateXsd. External resolution is disabled by default; enable a resolver only for trusted schema locations.
var schemas = new XmlSchemaSet
{
XmlResolver = new XmlUrlResolver(),
};
schemas.Add(null, schemaPath);
var result = document.ValidateXsd(schemas, new XsdValidationOptions
{
SchemaResolver = new XmlUrlResolver(),
});
Diagnostic locations refer to the current serialized DOM used for post-parse validation. A source URI is included when the document or configured schema provides one.
DOM model and querying
AngleSharp.Xml uses AngleSharp DOM interfaces and works with standard operations:
- QuerySelector / QuerySelectorAll
- CreateElement / CreateTextNode
- AppendChild / InsertBefore / Remove
- Attribute read and write methods
Example:
var item = document.QuerySelector("item");
item.SetAttribute("status", "active");
XML namespace semantics
Common attributes from the XML namespace have convenience APIs on elements and documents.
var item = document.QuerySelector("item");
var effectiveBaseUri = item.GetXmlBaseUri();
var effectiveBaseUrl = item.GetXmlBaseUrl();
var effectiveLanguage = item.GetXmlLanguage();
var xmlId = item.GetXmlId();
var target = document.GetElementByXmlId("chapter-1");
GetXmlBaseUri resolves inherited xml:base values against the document URL and returns non-ASCII LEIRI characters without escaping. GetXmlBaseUrl returns AngleSharp's URL representation, whose Href is URI-escaped.
GetXmlLanguage returns the nearest inherited xml:lang value. An empty value resets inherited language information and is returned as an empty string; null means no language was declared.
Parsed xml:id values receive ID whitespace normalization. GetElementByXmlId searches current DOM state in document order, so attribute mutations are reflected immediately.
XML declaration metadata
IXmlDocument exposes the parsed XML declaration. Documents without a declaration use XML 1.0 defaults and have a null XmlEncoding.
var document = parser.ParseDocument(
"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><root />");
Console.WriteLine(document.XmlVersion); // 1.0
Console.WriteLine(document.XmlEncoding); // utf-8
Console.WriteLine(document.XmlStandalone); // true
CDATA sections
Create XML-native CDATA sections through IXmlDocument. Parsed CDATA sections are also preserved as IXmlCDataSection nodes during DOM transformations and XML serialization.
var section = document.CreateCDataSection("<unescaped>content</unescaped>");
document.DocumentElement.AppendChild(section);
CDATA content cannot contain the closing delimiter ]]>. Creation and character-data mutations that would introduce it throw DomException without changing the section.
AngleSharp's core DOM does not expose entity reference nodes. CreateEntityReference therefore throws NotSupportedException; parsed entity references continue to be resolved to their replacement text.
DTD validity signal
When a document contains DOCTYPE declarations, AngleSharp.Xml evaluates DTD-related validity and exposes the result via document.IsValid.
var parser = new XmlParser();
var document = parser.ParseDocument(xmlText);
if (!document.IsValid)
{
Console.WriteLine("DTD validity check failed.");
}
For a detailed support matrix and examples, see the DTD Validation tutorial.
Events
XmlParser exposes parsing lifecycle events:
- Parsing
- Parsed
- Error
These are useful for diagnostics, telemetry, and observability in hosted applications.
1942246