JamesNK/Newtonsoft.Json · error · JsonSerializationException
Unexpected XmlNodeType when getting node name:
Error message
Unexpected XmlNodeType when getting node name:
What it means
Thrown by XmlNodeConverter.GetPropertyName's default case when an IXmlNode's NodeType is not among the handled element/attribute/PI/comment/text/whitespace types. The converter needs a JSON property name for each node; unhandled node types cannot be named and throw this JsonSerializationException. The message is left unterminated (no value appended) so the offending node type is not echoed.
Source
Thrown at Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs:1124
}
else
{
return ResolveFullName(node, manager);
}
case XmlNodeType.ProcessingInstruction:
return "?" + ResolveFullName(node, manager);
case XmlNodeType.DocumentType:
return "!" + ResolveFullName(node, manager);
case XmlNodeType.XmlDeclaration:
return DeclarationName;
case XmlNodeType.SignificantWhitespace:
return SignificantWhitespaceName;
case XmlNodeType.Text:
return TextName;
case XmlNodeType.Whitespace:
return WhitespaceName;
default:
throw new JsonSerializationException("Unexpected XmlNodeType when getting node name: " + node.NodeType);
}
}
private bool IsArray(IXmlNode node)
{
foreach (IXmlNode attribute in node.Attributes)
{
if (attribute.LocalName == "Array" && attribute.NamespaceUri == JsonNamespaceUri)
{
return XmlConvert.ToBoolean(attribute.Value!);
}
}
return false;
}
private void SerializeGroupedNodes(JsonWriter writer, IXmlNode node, XmlNamespaceManager manager, bool writePropertyName)
{View on GitHub (pinned to 4f73e74372)
Solutions
- Pre-process the XML to strip or normalize node types the converter does not support (entities, notations, etc.) before serialization.
- If using a custom IXmlNode wrapper, map unsupported node types to a supported one (e.g. Element/Text).
- Load the XML with settings that disable DTD/entity expansion to avoid exotic node types.
Example fix
// before: loading XML with DTD produces unsupported nodes
var doc = new XmlDocument(); doc.Load(xmlWithDtd);
JsonConvert.SerializeXmlNode(doc);
// after: load without DTD processing
var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
using var reader = XmlReader.Create(path, settings);
var doc = new XmlDocument(); doc.Load(reader);
JsonConvert.SerializeXmlNode(doc); Defensive patterns
Strategy: validation
Validate before calling
foreach (System.Xml.XmlNode node in doc.SelectNodes("//*"))
{
if (!IsSupportedForPropertyName(node.NodeType))
throw new InvalidDataException($"Unsupported XmlNodeType for naming: {node.NodeType}");
}
static bool IsSupportedForPropertyName(XmlNodeType t) =>
t == XmlNodeType.Attribute || t == XmlNodeType.CDATA || t == XmlNodeType.Comment
|| t == XmlNodeType.Element || t == XmlNodeType.ProcessingInstruction
|| t == XmlNodeType.DocumentType || t == XmlNodeType.XmlDeclaration
|| t == XmlNodeType.Text || t == XmlNodeType.Whitespace
|| t == XmlNodeType.SignificantWhitespace; Type guard
static bool IsNameableXmlNode(System.Xml.XmlNodeType t) =>
t == XmlNodeType.Element || t == XmlNodeType.Attribute || t == XmlNodeType.Comment
|| t == XmlNodeType.ProcessingInstruction || t == XmlNodeType.DocumentType
|| t == XmlNodeType.XmlDeclaration || t == XmlNodeType.Text
|| t == XmlNodeType.Whitespace || t == XmlNodeType.SignificantWhitespace
|| t == XmlNodeType.CDATA; Try / catch
try { JsonConvert.SerializeXmlNode(doc); }
catch (JsonSerializationException ex) when (ex.Message.Contains("Unexpected XmlNodeType when getting node name"))
{
throw new InvalidDataException("XML contains an unsupported node type for JSON naming", ex);
} Prevention
- Load XML with DtdProcessing.Ignore or Parse to avoid entity/notation nodes.
- Pre-normalize the DOM to supported node types before serialization.
- If wrapping XML with a custom IXmlNode, map node types to supported values.
When it happens
Trigger: Serializing an XML DOM containing a node type the converter does not map to a JSON key, e.g. XmlNodeType.Entity, XmlDataType-like nodes, or implementation-specific node types introduced by a custom IXmlNode wrapper.
Common situations: Loading an XML document with DTD/entity references that produce unusual node types. Using a custom IXmlNode implementation that returns a NodeType outside the handled set. Differences between the XML DOM on different runtimes.
Related errors
- Unexpected XmlNodeType when serializing nodes:
- Value must be an XML object.
- Namespace attribute must have a value.
- Unexpected value type when writing binary: {0}
- CustomCreationConverter should only be used while deserializ
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/a6999dfc059dcb01.
Report an issue: GitHub.