dotnet/wpf · error · FileFormatException

SR.XpsValidatingLoaderUnsupportedRootNamespaceUri

Error message

SR.XpsValidatingLoaderUnsupportedRootNamespaceUri

What it means

The first XML element read from an XPS part must belong to a namespace recognized by the part's schema (e.g. http://schemas.microsoft.com/xps/2005/06). If IsValidRootNamespaceUri returns false for the root element's NamespaceURI, Read() throws FileFormatException because the part content is not valid XPS markup of the declared MIME type.

Solutions

  1. Set the root element's xmlns to the correct XPS schema namespace for the part type (e.g. http://schemas.microsoft.com/xps/2005/06 for FixedPage).
  2. Confirm the part's Content Type matches its actual markup (a FixedDocumentSequence part must contain FixedDocumentSequence markup).
  3. Regenerate the part from a conformant XPS producer rather than hand-editing.
  4. Inspect the root element with a plain XmlReader before loading to verify NamespaceURI.

Example fix

// before
<Canvas xmlns="http://schemas.example.com/mycanvas">...</Canvas>
// after
<FixedPage xmlns="http://schemas.microsoft.com/xps/2005/06">...</FixedPage>
Defensive patterns

Strategy: validation

Validate before calling

static readonly string[] XpsNamespaces = { "http://schemas.microsoft.com/xps/2005/06", "http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key" };
using var xr = XmlReader.Create(part.GetStream());
xr.MoveToContent();
if (xr.NodeType == XmlNodeType.Element && !XpsNamespaces.Contains(xr.NamespaceURI))
    throw new InvalidDataException($"Root namespace {xr.NamespaceURI} is not a valid XPS root namespace");

Type guard

static bool HasValidXpsRootNamespace(XmlReader r) =>
    r.NodeType == XmlNodeType.Element &&
    r.NamespaceURI == "http://schemas.microsoft.com/xps/2005/06";

Try / catch

try { loader.Load(stream); }
catch (FileFormatException ex) { // root namespace rejected: log part URI + ContentType for diagnosis
    Console.WriteLine($"Part is not conformant XPS markup: {ex.Message}"); throw; }

Prevention

When it happens

Trigger: Loading an XPS part whose root element is in a foreign namespace — e.g. root <html>, an XHTML FixedPage, a ResourceDictionary in the wrong namespace, or a missing/typo'd xmlns — encountered by XpsSchemaValidator.Read() when NodeType==Element and the root namespace hasn't been checked yet.

Common situations: Hand-authored FixedPage markup missing the xmlns declaration; parts renamed to .fpage/.fdseq without changing content; third-party XPS generators emitting their own namespaces; version-mismatched markup (e.g. older 2005/06 vs 2007/07 schema URLs mixed up).

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedSchema.cs:214

            public override string GetAttribute( int i ) 
            {
                string attr = Reader.GetAttribute( i );
                CheckUri(attr);
                return attr;
            }

            public override bool Read() 
            {
                bool result;
                _node++;
                result = Reader.Read();

                if ( (Reader.NodeType == XmlNodeType.Element) && !_rootXMLNSChecked )
                {
                    if (!_schema.IsValidRootNamespaceUri(Reader.NamespaceURI))
                    {
                        throw new FileFormatException(SR.XpsValidatingLoaderUnsupportedRootNamespaceUri);
                    }
                    _rootXMLNSChecked = true;
                }

                return result;
            }

            private XpsValidatingLoader _loader;
            private XpsSchema _schema;
            private Uri _packageUri;
            private Uri _baseUri;
            private string _lastAttr;
            private int _node;
            private bool _rootXMLNSChecked;
        }
    }

View on GitHub (pinned to 81131a70a4)