dotnet/wpf · error · ArgumentException

ArgumentException(SR.StringIsNullOrEmpty, nameof(fileName))

Error message

ArgumentException(SR.StringIsNullOrEmpty, nameof(fileName))

What it means

XamlServices.Save(fileName, instance) serializes an object graph to XAML written to the file at fileName. Before opening the XmlWriter it validates fileName: null throws ArgumentNullException, and an empty (or whitespace-only, caught by IsNullOrEmpty here as the empty case) string throws ArgumentException with SR.StringIsNullOrEmpty because a file name is required to create the output stream.

Solutions

  1. Validate fileName is a non-empty string before calling Save
  2. Supply an explicit default output path when the configured path is empty
  3. Ensure the save-dialog/file-picker result is checked before invoking Save

Example fix

// before
XamlServices.Save(fileName, instance); // fileName == ""
// after
if (string.IsNullOrWhiteSpace(fileName))
    fileName = Path.Combine(AppContext.BaseDirectory, "output.xaml");
XamlServices.Save(fileName, instance);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(fileName)) throw new ArgumentException("File name must be non-empty", nameof(fileName));

Try / catch

try { XamlServices.Save(fileName, instance); }
catch (ArgumentException ex) when (ex.ParamName == "fileName") { /* prompt for a valid path */ }

Prevention

When it happens

Trigger: Calling XamlServices.Save(string fileName, object instance) with fileName equal to "" (e.g. an uninitialized or concatenation-produced empty path variable). Note the guard ThrowIfNull already handled null, so this branch fires for the empty string.

Common situations: Path built from an app setting or config value that is empty; user cancelled a save-file dialog and code proceeds with an empty filename; string.Split or Trim left the variable empty before the call.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlServices.cs:146

        public static string Save(object instance)
        {
            var sw = new StringWriter(CultureInfo.CurrentCulture);
            using (var xw = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }))
            {
                Save(xw, instance);
            }

            return sw.ToString();
        }

        public static void Save(string fileName, object instance)
        {
            ArgumentNullException.ThrowIfNull(fileName);
            //
            // At this point it can only be empty
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException(SR.StringIsNullOrEmpty, nameof(fileName));
            }

            using (var writer = XmlWriter.Create(fileName, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }))
            {
                Save(writer, instance);
                writer.Flush();
            }
        }

        public static void Save(Stream stream, object instance)
        {
            ArgumentNullException.ThrowIfNull(stream);
            using (var writer = XmlWriter.Create(stream, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }))
            {
                Save(writer, instance);
                writer.Flush();
            }
        }

View on GitHub (pinned to 81131a70a4)