JamesNK/Newtonsoft.Json · error · JsonSerializationException

Namespace attribute must have a value.

Error message

Namespace attribute must have a value.

What it means

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.

Source

Thrown at Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs:1322

                    if (IsArray(node) && AllSameName(node) && node.ChildNodes.Count > 0)
                    {
                        SerializeGroupedNodes(writer, node, manager, false);
                    }
                    else
                    {
                        manager.PushScope();

                        foreach (IXmlNode attribute in node.Attributes)
                        {
                            if (attribute.NamespaceUri == "http://www.w3.org/2000/xmlns/")
                            {
                                string namespacePrefix = (attribute.LocalName != "xmlns")
                                    ? XmlConvert.DecodeName(attribute.LocalName)!
                                    : string.Empty;
                                string? namespaceUri = attribute.Value;
                                if (namespaceUri == null)
                                {
                                    throw new JsonSerializationException("Namespace attribute must have a value.");
                                }

                                manager.AddNamespace(namespacePrefix, namespaceUri);
                            }
                        }

                        if (writePropertyName)
                        {
                            writer.WritePropertyName(GetPropertyName(node, manager));
                        }

                        if (!ValueAttributes(node.Attributes) && node.ChildNodes.Count == 1
                            && node.ChildNodes[0].NodeType == XmlNodeType.Text)
                        {
                            // write elements with a single text child as a name value pair
                            writer.WriteValue(node.ChildNodes[0].Value);
                        }
                        else if (node.ChildNodes.Count == 0 && node.Attributes.Count == 0)

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Ensure every xmlns/xmlns:prefix attribute in the XML has a non-null namespace URI value before serialization.
  2. If constructing the DOM in code, set the namespace string explicitly when adding the attribute.
  3. Sanitize the XML (remove or repair empty namespace attributes) before passing it to SerializeXmlNode/SerializeXNode.

Example fix

// before: adding an xmlns attribute with no value
var doc = new XmlDocument();
var el = doc.CreateElement("root");
var attr = doc.CreateAttribute("xmlns:myns");
// attr.Value left null
el.Attributes.Append(attr);

// after: supply a namespace URI
attr.Value = "http://example.com/myns";
el.Attributes.Append(attr);
Defensive patterns

Strategy: validation

Validate before calling

foreach (System.Xml.XmlAttribute a in doc.SelectNodes("//@xmlns:*") ?? new XmlNodeList()) { /* placeholder */ }
// Better: scan all attributes named xmlns or starting xmlns:
foreach (System.Xml.XmlNode el in doc.SelectNodes("//*"))
    foreach (System.Xml.XmlAttribute attr in ((System.Xml.XmlElement)el).Attributes)
        if ((attr.LocalName == "xmlns" || attr.Prefix == "xmlns") && string.IsNullOrEmpty(attr.Value))
            throw new InvalidDataException($"Namespace attribute '{attr.Name}' has no value");

Type guard

static bool NamespaceAttributesAllHaveValues(System.Xml.XmlNode doc)
{
    foreach (var el in doc.SelectNodes("//*").Cast<System.Xml.XmlElement>())
        foreach (System.Xml.XmlAttribute a in el.Attributes)
            if ((a.LocalName == "xmlns" || a.Prefix == "xmlns") && string.IsNullOrEmpty(a.Value))
                return false;
    return true;
}

Try / catch

try { JsonConvert.SerializeXmlNode(doc); }
catch (JsonSerializationException ex) when (ex.Message.Contains("Namespace attribute must have a value"))
{
    throw new InvalidDataException("XML has an xmlns attribute without a namespace URI", ex);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/4c0537f96fee9922. Report an issue: GitHub.