Practical Use Cases
This guide shows when AngleSharp.Xml is a good fit and how to structure real-world usage.
1. Configuration migration tool
Scenario: You need to update legacy XML configuration files to a new shape.
Approach:
- Parse each file with XmlParser
- Use selector-based discovery for target nodes
- Create missing elements and attributes
- Serialize with ToXml
C#
var parser = new XmlParser();
var document = parser.ParseDocument(xmlText);
foreach (var node in document.QuerySelectorAll("settings > add"))
{
if (node.GetAttribute("enabled") == null)
{
node.SetAttribute("enabled", "true");
}
}
var output = document.ToXml();
2. XML feed enrichment
Scenario: A service receives XML and must append calculated metadata.
Approach:
- Parse inbound XML
- Compute metadata
- Append extension nodes
- Return serialized XML
C#
var parser = new XmlParser();
var doc = parser.ParseDocument(feed);
var item = doc.QuerySelector("item");
var meta = doc.CreateElement("meta");
meta.SetAttribute("processedAt", DateTimeOffset.UtcNow.ToString("O"));
item.AppendChild(meta);
return doc.ToXml();
3. Unified HTML and XML processing pipeline
Scenario: The same backend processes HTML pages and XML documents.
Approach:
- Build one configuration with WithXml and other AngleSharp features
- Load inputs by content type
- Work with a consistent DOM API
C#
var config = Configuration.Default
.WithDefaultLoader()
.WithXml();
var context = BrowsingContext.New(config);
var xmlDoc = await context.OpenAsync(req =>
{
req.Content(xmlContent);
req.Header("Content-Type", "text/xml");
});
4. Diagnostics and source mapping
Scenario: You need to report precise XML errors in an editor or CI report.
Approach:
- Enable IsKeepingSourceReferences
- Capture OnCreated positions
- Map business rules back to line and column
C#
var parser = new XmlParser(new XmlParserOptions
{
IsKeepingSourceReferences = true,
OnCreated = (el, pos) => Console.WriteLine($"{el.NodeName} at {pos.Line}:{pos.Column}")
});
var doc = parser.ParseDocument(xmlText);
5. Namespace-safe processing
Scenario: Your input uses multiple namespace prefixes and xml-prefixed attributes.
Approach:
- Parse normally with XmlParser
- Read attributes by full name when needed
- Use NamespaceUri checks for robust routing logic
C#
var doc = new XmlParser().ParseDocument("<root xml:lang=\"en\" xmlns:ns=\"urn:demo\"><ns:item /></root>");
var lang = doc.DocumentElement.Attributes["xml:lang"];
if (lang?.NamespaceUri == NamespaceNames.XmlUri)
{
// Handle language-sensitive processing
}
Picking the right strategy
Choose this library first when your priority is:
- AngleSharp ecosystem integration
- DOM-based XML transformations
- Consistent programming style across markup types
Combine with specialized XML libraries when your priority is:
- Strict schema validation requirements
- XPath-dominant workflows
- Domain-specific XML standards validation
Edit this page on GitHub
View as Markdown
Synced from
1942246