dotnet/wpf · error · XmlException
XmlException
Error message
XmlException
What it means
XmlCompatibilityReader.Error is the internal helper that raises XmlException for any validation failure while reading compatibility-mode XAML (checking for unsupported nodes, bad syntax, or disallowed constructs under the current CompatibilityScope). It formats the message with the reader's current line/line position (defaulting to 1:1 when the underlying reader exposes no IXmlLineInfo), so every compatibility-check failure surfaces as System.Xml.XmlException.
Solutions
- Read the XmlException message and reported line/position and fix the markup at that exact location.
- Validate the XAML/XML file with an XML editor or linter before loading it.
- Check that the document follows the compatibility mode's allowed constructs (no unsupported nodes/attributes for that scope).
- If the error reports line 1 position 1, the underlying reader lacked line info — inspect the whole document for encoding issues (BOM, invalid characters).
- Re-encode the file as UTF-8 and re-save it, then retry the load.
Example fix
// before (mismatched tag) <StackPanel> <Button>Click</TextBlock> </StackPanel> // after <StackPanel> <Button>Click</Button> </StackPanel>
Defensive patterns
Strategy: try-catch
Validate before calling
// Validate XML well-formedness before handing it to the XAML reader
try
{
using var reader = XmlReader.Create(new StringReader(xamlText));
while (reader.Read()) { }
}
catch (XmlException ex)
{
throw new FormatException($"Invalid markup at line {ex.LineNumber}, position {ex.LinePosition}: {ex.Message}", ex);
} Type guard
static bool IsWellFormedXml(string xml)
{
try { using var r = System.Xml.XmlReader.Create(new System.IO.StringReader(xml)); while (r.Read()) { } return true; }
catch (System.Xml.XmlException) { return false; }
} Try / catch
try
{
XamlReader.Load(xamlStream);
}
catch (System.Xml.XmlException ex)
{
Console.Error.WriteLine($"Markup error at {ex.LineNumber}:{ex.LinePosition}: {ex.Message}");
} Prevention
- Validate XAML files in CI with an XML/XAML linter before deployment.
- Edit XAML in an editor with schema-aware validation rather than plain text editors.
- Re-save files as UTF-8 and avoid hand-editing generated XAML.
- Log LineNumber/LinePosition from XmlException to pinpoint defects quickly.
When it happens
Trigger: Any malformed or unsupported XML construct read by XmlCompatibilityReader: invalid element/attribute names, unsupported DTD or processing instructions, mismatched tags, or content not allowed in compatibility mode — each Error(...) call site funnels here to throw XmlException with line info, XmlCompatibilityReader.cs:1476-1482.
Common situations: Hand-edited XAML with typos or mismatched tags; XML saved with unsupported encodings or constructs (undefined entities, invalid characters); documents written for a different XAML compatibility level; copy-pasted markup containing invalid syntax.
Related errors
- MappingParseError(_scanner.Start, token, _token)
- message (InvalidOperationException)
- SR.CannotParseId
- SR.ParserMultiRoot
- SR.RequiresXmlNamespaceMapping (formatted with value type…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9d2f2ed5db5a175a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/XmlCompatibilityReader.cs:1481
/// functionality is supported, but not implemented
/// </summary>
private void HandlePreserveAttributes(int elementDepth)
{
PushScope(elementDepth);
foreach (NamespaceElementPair pair in ParseContentToNamespaceElementPair(Reader.Value, _preserveAttributes))
{
Scope.PreserveAttribute(pair.namespaceName, pair.itemName);
}
}
/// <summary>
/// helper method to generate an exception
/// </summary>
private void Error(string message, params object[] args)
{
IXmlLineInfo info = Reader as IXmlLineInfo;
throw new XmlException(string.Format(CultureInfo.InvariantCulture, message, args), null, info is null ? 1 : info.LineNumber,
info is null ? 1 : info.LinePosition);
}
#endregion Private Methods
#region Private Properties
private CompatibilityScope Scope
{
get
{
return _compatibilityScope;
}
}
private string AlternateContent
{
get
{
if (_alternateContent is null)View on GitHub (pinned to 81131a70a4)