{"record":{"id":"a6999dfc059dcb01","repo":"JamesNK/Newtonsoft.Json","slug":"unexpected-xmlnodetype-when-getting-node-name","errorCode":null,"errorMessage":"Unexpected XmlNodeType when getting node name: ","messagePattern":"Unexpected XmlNodeType when getting node name: ","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs","lineNumber":1124,"sourceCode":"                    }\n                    else\n                    {\n                        return ResolveFullName(node, manager);\n                    }\n                case XmlNodeType.ProcessingInstruction:\n                    return \"?\" + ResolveFullName(node, manager);\n                case XmlNodeType.DocumentType:\n                    return \"!\" + ResolveFullName(node, manager);\n                case XmlNodeType.XmlDeclaration:\n                    return DeclarationName;\n                case XmlNodeType.SignificantWhitespace:\n                    return SignificantWhitespaceName;\n                case XmlNodeType.Text:\n                    return TextName;\n                case XmlNodeType.Whitespace:\n                    return WhitespaceName;\n                default:\n                    throw new JsonSerializationException(\"Unexpected XmlNodeType when getting node name: \" + node.NodeType);\n            }\n        }\n\n        private bool IsArray(IXmlNode node)\n        {\n            foreach (IXmlNode attribute in node.Attributes)\n            {\n                if (attribute.LocalName == \"Array\" && attribute.NamespaceUri == JsonNamespaceUri)\n                {\n                    return XmlConvert.ToBoolean(attribute.Value!);\n                }\n            }\n\n            return false;\n        }\n\n        private void SerializeGroupedNodes(JsonWriter writer, IXmlNode node, XmlNamespaceManager manager, bool writePropertyName)\n        {","sourceCodeStart":1106,"sourceCodeEnd":1142,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs#L1106-L1142","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before: loading XML with DTD produces unsupported nodes\nvar doc = new XmlDocument(); doc.Load(xmlWithDtd);\nJsonConvert.SerializeXmlNode(doc);\n\n// after: load without DTD processing\nvar settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };\nusing var reader = XmlReader.Create(path, settings);\nvar doc = new XmlDocument(); doc.Load(reader);\nJsonConvert.SerializeXmlNode(doc);","handlingStrategy":"validation","validationCode":"foreach (System.Xml.XmlNode node in doc.SelectNodes(\"//*\"))\n{\n    if (!IsSupportedForPropertyName(node.NodeType))\n        throw new InvalidDataException($\"Unsupported XmlNodeType for naming: {node.NodeType}\");\n}\n\nstatic bool IsSupportedForPropertyName(XmlNodeType t) =>\n    t == XmlNodeType.Attribute || t == XmlNodeType.CDATA || t == XmlNodeType.Comment\n    || t == XmlNodeType.Element || t == XmlNodeType.ProcessingInstruction\n    || t == XmlNodeType.DocumentType || t == XmlNodeType.XmlDeclaration\n    || t == XmlNodeType.Text || t == XmlNodeType.Whitespace\n    || t == XmlNodeType.SignificantWhitespace;","typeGuard":"static bool IsNameableXmlNode(System.Xml.XmlNodeType t) =>\n    t == XmlNodeType.Element || t == XmlNodeType.Attribute || t == XmlNodeType.Comment\n    || t == XmlNodeType.ProcessingInstruction || t == XmlNodeType.DocumentType\n    || t == XmlNodeType.XmlDeclaration || t == XmlNodeType.Text\n    || t == XmlNodeType.Whitespace || t == XmlNodeType.SignificantWhitespace\n    || t == XmlNodeType.CDATA;","tryCatchPattern":"try { JsonConvert.SerializeXmlNode(doc); }\ncatch (JsonSerializationException ex) when (ex.Message.Contains(\"Unexpected XmlNodeType when getting node name\"))\n{\n    throw new InvalidDataException(\"XML contains an unsupported node type for JSON naming\", ex);\n}","preventionTips":["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."],"tags":["serialization","converter","xml","node-type"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}