{"record":{"id":"de47fe25433f33dd","repo":"dotnet/wpf","slug":"sr-textrange-unsupporteddataformat-dataformat","errorCode":null,"errorMessage":"SR.TextRange_UnsupportedDataFormat (dataFormat)","messagePattern":"SR\\.TextRange_UnsupportedDataFormat \\(dataFormat\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextRangeBase.cs","lineNumber":1538,"sourceCode":"                // independently whether there are images in it or not.\n                WpfPayload.SaveRange(thisRange, ref stream, /*useFlowDocumentAsRoot:*/false, preserveTextElements);\n            }\n            else if (dataFormat == DataFormats.Rtf)\n            {\n                Stream wpfPayloadMemory = null;\n                // Passing null as a wpfPayloadStream we allow to not create wpf package\n                // when it is not needed (there is no images in the range)\n                string xamlText = WpfPayload.SaveRange(thisRange, ref wpfPayloadMemory, /*useFlowDocumentAsRoot:*/false);\n                // Convert xaml to rtf text to set rtf data into data object.\n                string rtfText = TextEditorCopyPaste.ConvertXamlToRtf(xamlText, wpfPayloadMemory);\n                StreamWriter rtfStreamWriter = new StreamWriter(stream);\n                rtfStreamWriter.Write(rtfText);\n                rtfStreamWriter.Flush();\n            }\n            else\n            {\n                // Unsupported format - thows exception\n                throw new ArgumentException(SR.Format(SR.TextRange_UnsupportedDataFormat, dataFormat), nameof(dataFormat));\n            }\n        }\n\n        internal static void Load(TextRange thisRange, Stream stream, string dataFormat)\n        {\n            ArgumentNullException.ThrowIfNull(stream);\n            ArgumentNullException.ThrowIfNull(dataFormat);\n\n            NormalizeRange(thisRange);\n\n            // Reset the stream position to the beginning\n            if (stream.CanSeek)\n            {\n                stream.Seek(0, SeekOrigin.Begin);\n            }\n\n            if (dataFormat == DataFormats.Text)\n            {","sourceCodeStart":1520,"sourceCodeEnd":1556,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextRangeBase.cs#L1520-L1556","documentation":"TextRangeBase.Save throws ArgumentException when the dataFormat string passed for saving the range content is not one of the supported formats (DataFormats.Xaml, XamlPackage, Rtf). The message includes the unsupported format name. This guards the serialization switch in the Save path.","triggerScenarios":"Calling TextRange.Save(stream, dataFormat) with formats like DataFormats.Text, Html, or custom strings — the Save path only writes Xaml, XamlPackage, and Rtf.","commonSituations":"Assuming Save supports the same formats as Load/Clipboard; passing DataFormats.UnicodeText or Html expecting plain/HTML output; programmatic export code with a typo in the format string.","solutions":["Use only \"Xaml\", \"XamlPackage\", or \"Rtf\" (DataFormats.Xaml / DataFormats.XamlPackage / DataFormats.Rtf) with TextRange.Save","For plain text, extract range.Text and write it yourself","Validate the format string against the supported list before calling"],"exampleFix":"// before\nrange.Save(stream, DataFormats.Text);\n// after\nrange.Save(stream, DataFormats.Xaml);","handlingStrategy":"validation","validationCode":"var supported = new[]{ DataFormats.Xaml, DataFormats.XamlPackage, DataFormats.Rtf };\nif (!supported.Contains(dataFormat)) throw new NotSupportedException(dataFormat);","typeGuard":"bool IsSaveableFormat(string f) => f is DataFormats.Xaml or DataFormats.XamlPackage or DataFormats.Rtf;","tryCatchPattern":"try { range.Save(stream, dataFormat); } catch (ArgumentException ex) { /* unsupported save format */ }","preventionTips":["Restrict save formats to Xaml/XamlPackage/Rtf","Write plain text via range.Text instead"],"tags":["wpf","clipboard","serialization"],"backgroundTag":"unsupported-operation","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"}