{"record":{"id":"a48760a0bd942437","repo":"dotnet/wpf","slug":"printschematags-framework-printticketroot-ptutility","errorCode":null,"errorMessage":"PrintSchemaTags.Framework.PrintTicketRoot + PTUtility.GetTextFromResource(\"FormatException.XMLNotWellFormed\") + xmlException.Message","messagePattern":"PrintSchemaTags\\.Framework\\.PrintTicketRoot \\+ PTUtility\\.GetTextFromResource\\(\"FormatException\\.XMLNotWellFormed\"\\) \\+ xmlException\\.Message","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/FallbackPTProvider.cs","lineNumber":108,"sourceCode":"        /// </exception>\n        /// <exception cref=\"ArgumentException\">\n        /// The input PrintTicket specified by <paramref name=\"printTicket\"/> is not well-formed.\n        /// </exception>\n        /// <exception cref=\"PrintQueueException\">\n        /// The PTProvider instance failed to retrieve the PrintCapabilities.\n        /// </exception>\n        public override MemoryStream GetPrintCapabilities(MemoryStream printTicket)\n        {\n            VerifyAccess();\n\n            InternalPrintTicket internalTicket = null;\n            try\n            {\n                internalTicket = (printTicket != null) ? new InternalPrintTicket(printTicket) : null;\n            }\n            catch (XmlException xmlException)\n            {\n                throw new ArgumentException(\n                    String.Format(\n                        CultureInfo.CurrentCulture,\n                        \"{0} {1} {2}\",\n                        PrintSchemaTags.Framework.PrintTicketRoot,\n                        PTUtility.GetTextFromResource(\"FormatException.XMLNotWellFormed\"),\n                        xmlException.Message),\n                    nameof(printTicket),\n                    xmlException);\n            }\n\n            DevMode defaultDevMode = GetDEVMODE(BaseDevModeType.UserDefault);\n            DevMode devMode = defaultDevMode.Clone();\n\n            PrintTicketToDevMode(devMode, internalTicket, PrintTicketScope.JobScope, DevModeFields.All);\n\n            MemoryStream capabilitiesStream = new MemoryStream();\n\n            WinSpoolPrinterCapabilities capabilities = GetCapabilities(devMode);","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/FallbackPTProvider.cs#L90-L126","documentation":"GetPrintCapabilities received a PrintTicket whose XML failed to parse (XmlException) while wrapping it in an InternalPrintTicket. The API surfaces this as an ArgumentException whose message starts with the PrintTicket root element name followed by the 'XML not well formed' resource text and the parser's message.","triggerScenarios":"Calling FallbackPTProvider.GetPrintCapabilities(printTicket) with a printTicket MemoryStream/XmlReader containing malformed XML (bad encoding, unclosed tags, invalid characters), so new InternalPrintTicket(printTicket) throws XmlException.","commonSituations":"Hand-edited or template-generated PrintTicket XML; stream position not reset before passing to the API; wrong encoding declaration; truncated ticket saved to a stream.","solutions":["Validate the PrintTicket XML is well formed (parse it with XmlDocument/XDocument) before calling GetPrintCapabilities.","Reset the stream position to 0 before passing a MemoryStream to the API.","Ensure the root element is PrintTicket from the print schema namespace and the XML declaration matches the actual encoding.","Catch ArgumentException around GetPrintCapabilities and inspect the message for the underlying XmlException detail."],"exampleFix":"// before\nstream.Position = 0; // often forgotten\ncaps = provider.GetPrintCapabilities(printTicket);\n// after\nstream.Position = 0;\nnew XDocument(XDocument.Load(stream)) ; // throws early with a clear parser message\nstream.Position = 0;\ncaps = provider.GetPrintCapabilities(printTicket);","handlingStrategy":"validation","validationCode":"stream.Position = 0;\ntry { new XDocument(XDocument.Load(stream, LoadOptions.None)); }\ncatch (XmlException ex) { throw new InvalidOperationException(\"PrintTicket XML is not well formed: \" + ex.Message, ex); }\nfinally { stream.Position = 0; }","typeGuard":"static bool IsWellFormedXml(Stream s)\n{\n    var pos = s.Position; s.Position = 0;\n    try { XDocument.Load(s); return true; }\n    catch (XmlException) { return false; }\n    finally { s.Position = pos; }\n}","tryCatchPattern":"try { caps = provider.GetPrintCapabilities(printTicket); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"not well formed\"))\n{\n    // regenerate a default ticket for the queue\n}","preventionTips":["Always reset Stream.Position to 0 before passing ticket streams.","Validate ticket XML well-formedness with XDocument.Load before use.","Never hand-edit PrintTicket XML without re-parsing it afterwards."],"tags":["wpf","printing","xml","argumentexception"],"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"}