dotnet/wpf · error · InvalidDataException
SR.DocumentStreamMustBeXpsFile
Error message
SR.DocumentStreamMustBeXpsFile
What it means
ThrowIfInvalidXpsFileForSave throws InvalidDataException when the target file's extension is not .xps (case-insensitive comparison against XpsFileExtension). Save operations require the destination to be an XPS file; saving to a non-XPS extension would produce an incorrectly named package.
Solutions
- Ensure the save target path ends with the .xps extension before initiating the save.
- When generating temp file names, append .xps (e.g. Path.ChangeExtension(path, ".xps")).
- Validate user-supplied file names in the save dialog and correct/append the extension.
Example fix
// before var path = Path.Combine(tempDir, Guid.NewGuid().ToString()); // no extension // after var path = Path.ChangeExtension(Path.Combine(tempDir, Guid.NewGuid().ToString()), ".xps");
Defensive patterns
Strategy: validation
Validate before calling
bool isXpsTarget = string.Equals(Path.GetExtension(location.LocalPath), ".xps", StringComparison.OrdinalIgnoreCase);
Try / catch
try { SaveTo(location); }
catch (InvalidDataException) { location = new Uri(Path.ChangeExtension(location.LocalPath, ".xps")); SaveTo(location); } Prevention
- Append .xps to every generated temp filename used as a save target.
- Sanitize Save dialog file names to enforce the .xps extension.
When it happens
Trigger: Save/commit flows (Copy, SwapWithOriginal, MakeTempFile) given a location URI whose LocalPath extension is not '.xps', e.g. '.zip', '.oxps', or a no-extension temp name.
Common situations: Users typing a filename with a wrong or missing extension in a Save dialog; code generating temp file names without appending .xps; renaming documents to .zip to inspect contents then trying to save back.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- " }} " element found. Expected fixed page element ( }} ).
- ' ' ContentType is not valid.
- ' ' ContentType is not valid.
- Buffer address passed to GetText cannot be NULL.
- Cannot access the stream after it is closed.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/11729e51e437a12f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/DocumentStream.cs:958
e);
}
}
/// <summary>
/// Will throw if location is not a XpsDocument file.
/// </summary>
/// <exception cref="System.ArgumentNullException"/>
/// <exception cref="System.InvalidOperationException"/>
/// <exception cref="System.InvalidDataException"/>
/// <param name="location"></param>
private static void ThrowIfInvalidXpsFileForSave(Uri location)
{
ThrowIfInvalidXpsFileForOpen(location);
if (!Path.GetExtension(location.LocalPath).Equals(
XpsFileExtension, StringComparison.OrdinalIgnoreCase))
{
throw new InvalidDataException(
SR.DocumentStreamMustBeXpsFile);
}
}
/// <summary>
/// Will throw if location is not a valid file.
/// </summary>
/// <exception cref="System.ArgumentNullException"/>
/// <exception cref="System.InvalidOperationException"/>
private static void ThrowIfInvalidXpsFileForOpen(Uri location)
{
ArgumentNullException.ThrowIfNull(location);
if (!location.IsFile)
{
throw new InvalidOperationException(
SR.DocumentStreamMustBeFileSource);
}
}View on GitHub (pinned to 81131a70a4)