{"record":{"id":"a688b19008f162b4","repo":"dotnet/wpf","slug":"argumentexception-sr-stringisnullorempty-nameof-filename","errorCode":null,"errorMessage":"ArgumentException(SR.StringIsNullOrEmpty, nameof(fileName))","messagePattern":"ArgumentException\\(SR\\.StringIsNullOrEmpty, nameof\\(fileName\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlServices.cs","lineNumber":146,"sourceCode":"        public static string Save(object instance)\n        {\n            var sw = new StringWriter(CultureInfo.CurrentCulture);\n            using (var xw = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }))\n            {\n                Save(xw, instance);\n            }\n\n            return sw.ToString();\n        }\n\n        public static void Save(string fileName, object instance)\n        {\n            ArgumentNullException.ThrowIfNull(fileName);\n            //\n            // At this point it can only be empty\n            if (string.IsNullOrEmpty(fileName))\n            {\n                throw new ArgumentException(SR.StringIsNullOrEmpty, nameof(fileName));\n            }\n\n            using (var writer = XmlWriter.Create(fileName, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }))\n            {\n                Save(writer, instance);\n                writer.Flush();\n            }\n        }\n\n        public static void Save(Stream stream, object instance)\n        {\n            ArgumentNullException.ThrowIfNull(stream);\n            using (var writer = XmlWriter.Create(stream, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }))\n            {\n                Save(writer, instance);\n                writer.Flush();\n            }\n        }","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlServices.cs#L128-L164","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate fileName is a non-empty string before calling Save","Supply an explicit default output path when the configured path is empty","Ensure the save-dialog/file-picker result is checked before invoking Save"],"exampleFix":"// before\nXamlServices.Save(fileName, instance); // fileName == \"\"\n// after\nif (string.IsNullOrWhiteSpace(fileName))\n    fileName = Path.Combine(AppContext.BaseDirectory, \"output.xaml\");\nXamlServices.Save(fileName, instance);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(fileName)) throw new ArgumentException(\"File name must be non-empty\", nameof(fileName));","typeGuard":null,"tryCatchPattern":"try { XamlServices.Save(fileName, instance); }\ncatch (ArgumentException ex) when (ex.ParamName == \"fileName\") { /* prompt for a valid path */ }","preventionTips":["Validate file paths with string.IsNullOrWhiteSpace before saving","Check dialog results before proceeding with a save","Default empty configured paths to a known output location"],"tags":["argumentexception","empty-string","filename","xaml"],"backgroundTag":"empty-required-field","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}