dotnet/wpf · error · ArgumentException

SR.ParserWriterUnknownOrigin

Error message

SR.ParserWriterUnknownOrigin

What it means

XamlStream's WriterSeek validates the SeekOrigin value; any value other than Begin, Current, or End reaches the default branch and throws ArgumentException(SR.ParserWriterUnknownOrigin). This indicates an invalid SeekOrigin argument.

Solutions

  1. Pass a defined SeekOrigin value: SeekOrigin.Begin, SeekOrigin.Current, or SeekOrigin.End.
  2. Validate any externally supplied origin integer against Enum.IsDefined(typeof(SeekOrigin), value) before casting.
  3. Fix the caller that computes the origin value.

Example fix

// before
stream.Seek(offset, (SeekOrigin)originInt); // originInt = 9
// after
if (!Enum.IsDefined(typeof(SeekOrigin), originInt)) throw new ArgumentException("bad origin");
stream.Seek(offset, (SeekOrigin)originInt);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(SeekOrigin), origin)) throw new ArgumentException(nameof(origin));

Type guard

bool validOrigin = origin is SeekOrigin.Begin or SeekOrigin.Current or SeekOrigin.End;

Try / catch

try { stream.Seek(offset, origin); }
catch (ArgumentException ex) when (ex.Message.Contains("origin")) { /* fix origin value */ }

Prevention

When it happens

Trigger: Calling Seek(offset, (SeekOrigin)someInt) on the XamlStream write side with an int that is not a defined SeekOrigin member (e.g. 3 or a negative/cast value).

Common situations: Origin value loaded from config/data or computed by cast instead of using SeekOrigin enum members; corrupted interop code passing a bad enum value.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/012699819ceff2de. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs:155

        /// Seek past the writeLength.
        /// </summary>
        /// <param name="offset">seek offset</param>
        /// <param name="loc">specifies how to interpret the seeek offset</param>
        /// <returns></returns>
        internal long WriterSeek(long offset, SeekOrigin loc)
        {
            switch(loc)
            {
                case SeekOrigin.Begin:
                    WritePosition = (int) offset;
                    break;
                case SeekOrigin.Current:
                    WritePosition = (int) (WritePosition + offset);
                    break;
                case SeekOrigin.End:
                    throw new NotSupportedException(SR.ParserWriterNoSeekEnd);
                default:
                    throw new ArgumentException(SR.ParserWriterUnknownOrigin);
            }

            if( (!( WritePosition <= WriteLength ))
                ||
                (!( WritePosition >= ReadLength  )) )
            {
                throw new ArgumentOutOfRangeException( nameof(offset));
            }

            return WritePosition;
        }


        /// <summary>
        /// Called by the Writer to indicate its okay for the reader to see
        /// the file up to and including the position. Once the Writer calls
        /// this it cannot go back and change the content.
        /// </summary>

View on GitHub (pinned to 81131a70a4)