dotnet/wpf · error · ArgumentException
SR.TextRange_UnsupportedDataFormat (dataFormat)
Error message
SR.TextRange_UnsupportedDataFormat (dataFormat)
What it means
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.
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
Example fix
// before range.Save(stream, DataFormats.Text); // after range.Save(stream, DataFormats.Xaml);
Defensive patterns
Strategy: validation
Validate before calling
var supported = new[]{ DataFormats.Xaml, DataFormats.XamlPackage, DataFormats.Rtf };
if (!supported.Contains(dataFormat)) throw new NotSupportedException(dataFormat); Type guard
bool IsSaveableFormat(string f) => f is DataFormats.Xaml or DataFormats.XamlPackage or DataFormats.Rtf;
Try / catch
try { range.Save(stream, dataFormat); } catch (ArgumentException ex) { /* unsupported save format */ } Prevention
- Restrict save formats to Xaml/XamlPackage/Rtf
- Write plain text via range.Text instead
When it happens
Trigger: Calling TextRange.Save(stream, dataFormat) with formats like DataFormats.Text, Html, or custom strings — the Save path only writes Xaml, XamlPackage, and Rtf.
Common situations: 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.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- ArgumentNullException(nameof(writer))
- ArgumentOutOfRangeException(routedEvent)
- Cannot find the appropriate serializer.
- Cannot find the appropriate serializer.
- Cannot find the appropriate serializer.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/de47fe25433f33dd.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextRangeBase.cs:1538
// independently whether there are images in it or not.
WpfPayload.SaveRange(thisRange, ref stream, /*useFlowDocumentAsRoot:*/false, preserveTextElements);
}
else if (dataFormat == DataFormats.Rtf)
{
Stream wpfPayloadMemory = null;
// Passing null as a wpfPayloadStream we allow to not create wpf package
// when it is not needed (there is no images in the range)
string xamlText = WpfPayload.SaveRange(thisRange, ref wpfPayloadMemory, /*useFlowDocumentAsRoot:*/false);
// Convert xaml to rtf text to set rtf data into data object.
string rtfText = TextEditorCopyPaste.ConvertXamlToRtf(xamlText, wpfPayloadMemory);
StreamWriter rtfStreamWriter = new StreamWriter(stream);
rtfStreamWriter.Write(rtfText);
rtfStreamWriter.Flush();
}
else
{
// Unsupported format - thows exception
throw new ArgumentException(SR.Format(SR.TextRange_UnsupportedDataFormat, dataFormat), nameof(dataFormat));
}
}
internal static void Load(TextRange thisRange, Stream stream, string dataFormat)
{
ArgumentNullException.ThrowIfNull(stream);
ArgumentNullException.ThrowIfNull(dataFormat);
NormalizeRange(thisRange);
// Reset the stream position to the beginning
if (stream.CanSeek)
{
stream.Seek(0, SeekOrigin.Begin);
}
if (dataFormat == DataFormats.Text)
{View on GitHub (pinned to 81131a70a4)