{"record":{"id":"4c0537f96fee9922","repo":"JamesNK/Newtonsoft.Json","slug":"namespace-attribute-must-have-a-value","errorCode":null,"errorMessage":"Namespace attribute must have a value.","messagePattern":"Namespace attribute must have a value\\.","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs","lineNumber":1322,"sourceCode":"                    if (IsArray(node) && AllSameName(node) && node.ChildNodes.Count > 0)\n                    {\n                        SerializeGroupedNodes(writer, node, manager, false);\n                    }\n                    else\n                    {\n                        manager.PushScope();\n\n                        foreach (IXmlNode attribute in node.Attributes)\n                        {\n                            if (attribute.NamespaceUri == \"http://www.w3.org/2000/xmlns/\")\n                            {\n                                string namespacePrefix = (attribute.LocalName != \"xmlns\")\n                                    ? XmlConvert.DecodeName(attribute.LocalName)!\n                                    : string.Empty;\n                                string? namespaceUri = attribute.Value;\n                                if (namespaceUri == null)\n                                {\n                                    throw new JsonSerializationException(\"Namespace attribute must have a value.\");\n                                }\n\n                                manager.AddNamespace(namespacePrefix, namespaceUri);\n                            }\n                        }\n\n                        if (writePropertyName)\n                        {\n                            writer.WritePropertyName(GetPropertyName(node, manager));\n                        }\n\n                        if (!ValueAttributes(node.Attributes) && node.ChildNodes.Count == 1\n                            && node.ChildNodes[0].NodeType == XmlNodeType.Text)\n                        {\n                            // write elements with a single text child as a name value pair\n                            writer.WriteValue(node.ChildNodes[0].Value);\n                        }\n                        else if (node.ChildNodes.Count == 0 && node.Attributes.Count == 0)","sourceCodeStart":1304,"sourceCodeEnd":1340,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs#L1304-L1340","documentation":"Thrown by XmlNodeConverter.SerializeNode when iterating an element's attributes and finding an xmlns namespace declaration whose Value is null. A namespace attribute must resolve to a URI; a null value is invalid and the converter cannot register the namespace prefix.","triggerScenarios":"Serializing an XML DOM where an xmlns:prefix attribute exists but has a null/empty value. This can arise from programmatic DOM construction that sets an xmlns attribute without a value, or from malformed XML that a lenient parser accepted.","commonSituations":"Building an XmlDocument/XElement in code and adding an xmlns attribute via SetAttribute without supplying a namespace string. XSLT transforms or XML merging that produces dangling namespace declarations. Parsing partially-malformed XML.","solutions":["Ensure every xmlns/xmlns:prefix attribute in the XML has a non-null namespace URI value before serialization.","If constructing the DOM in code, set the namespace string explicitly when adding the attribute.","Sanitize the XML (remove or repair empty namespace attributes) before passing it to SerializeXmlNode/SerializeXNode."],"exampleFix":"// before: adding an xmlns attribute with no value\nvar doc = new XmlDocument();\nvar el = doc.CreateElement(\"root\");\nvar attr = doc.CreateAttribute(\"xmlns:myns\");\n// attr.Value left null\nel.Attributes.Append(attr);\n\n// after: supply a namespace URI\nattr.Value = \"http://example.com/myns\";\nel.Attributes.Append(attr);","handlingStrategy":"validation","validationCode":"foreach (System.Xml.XmlAttribute a in doc.SelectNodes(\"//@xmlns:*\") ?? new XmlNodeList()) { /* placeholder */ }\n// Better: scan all attributes named xmlns or starting xmlns:\nforeach (System.Xml.XmlNode el in doc.SelectNodes(\"//*\"))\n    foreach (System.Xml.XmlAttribute attr in ((System.Xml.XmlElement)el).Attributes)\n        if ((attr.LocalName == \"xmlns\" || attr.Prefix == \"xmlns\") && string.IsNullOrEmpty(attr.Value))\n            throw new InvalidDataException($\"Namespace attribute '{attr.Name}' has no value\");","typeGuard":"static bool NamespaceAttributesAllHaveValues(System.Xml.XmlNode doc)\n{\n    foreach (var el in doc.SelectNodes(\"//*\").Cast<System.Xml.XmlElement>())\n        foreach (System.Xml.XmlAttribute a in el.Attributes)\n            if ((a.LocalName == \"xmlns\" || a.Prefix == \"xmlns\") && string.IsNullOrEmpty(a.Value))\n                return false;\n    return true;\n}","tryCatchPattern":"try { JsonConvert.SerializeXmlNode(doc); }\ncatch (JsonSerializationException ex) when (ex.Message.Contains(\"Namespace attribute must have a value\"))\n{\n    throw new InvalidDataException(\"XML has an xmlns attribute without a namespace URI\", ex);\n}","preventionTips":["When constructing the DOM, always supply a URI for xmlns attributes.","Validate namespace attributes have non-empty values before serialization.","Avoid adding xmlns attributes via raw SetAttribute without a value."],"tags":["serialization","converter","xml","namespace","validation"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}