{"record":{"id":"9d2f2ed5db5a175a","repo":"dotnet/wpf","slug":"xmlexception","errorCode":null,"errorMessage":"XmlException","messagePattern":"XmlException","errorType":"exception","errorClass":"XmlException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/XmlCompatibilityReader.cs","lineNumber":1481,"sourceCode":"        /// functionality is supported, but not implemented\n        /// </summary>\n        private void HandlePreserveAttributes(int elementDepth)\n        {\n            PushScope(elementDepth);\n\n            foreach (NamespaceElementPair pair in ParseContentToNamespaceElementPair(Reader.Value, _preserveAttributes))\n            {\n                Scope.PreserveAttribute(pair.namespaceName, pair.itemName);\n            }\n        }\n\n        /// <summary>\n        /// helper method to generate an exception\n        /// </summary>\n        private void Error(string message, params object[] args)\n        {\n            IXmlLineInfo info = Reader as IXmlLineInfo;\n            throw new XmlException(string.Format(CultureInfo.InvariantCulture, message, args), null, info is null ? 1 : info.LineNumber,\n                info is null ? 1 : info.LinePosition);\n        }\n        #endregion Private Methods\n\n        #region Private Properties\n        private CompatibilityScope Scope\n        {\n            get\n            {\n                return _compatibilityScope;\n            }\n        }\n\n        private string AlternateContent\n        {\n            get\n            {\n                if (_alternateContent is null)","sourceCodeStart":1463,"sourceCodeEnd":1499,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/System/Windows/Markup/XmlCompatibilityReader.cs#L1463-L1499","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (mismatched tag)\n<StackPanel>\n  <Button>Click</TextBlock>\n</StackPanel>\n\n// after\n<StackPanel>\n  <Button>Click</Button>\n</StackPanel>","handlingStrategy":"try-catch","validationCode":"// Validate XML well-formedness before handing it to the XAML reader\ntry\n{\n    using var reader = XmlReader.Create(new StringReader(xamlText));\n    while (reader.Read()) { }\n}\ncatch (XmlException ex)\n{\n    throw new FormatException($\"Invalid markup at line {ex.LineNumber}, position {ex.LinePosition}: {ex.Message}\", ex);\n}","typeGuard":"static bool IsWellFormedXml(string xml)\n{\n    try { using var r = System.Xml.XmlReader.Create(new System.IO.StringReader(xml)); while (r.Read()) { } return true; }\n    catch (System.Xml.XmlException) { return false; }\n}","tryCatchPattern":"try\n{\n    XamlReader.Load(xamlStream);\n}\ncatch (System.Xml.XmlException ex)\n{\n    Console.Error.WriteLine($\"Markup error at {ex.LineNumber}:{ex.LinePosition}: {ex.Message}\");\n}","preventionTips":["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."],"tags":["wpf","xaml","xml","parse-error","reader"],"backgroundTag":"xml-parse-error","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}