{"record":{"id":"14b30a331b249217","repo":"JamesNK/Newtonsoft.Json","slug":"value-must-be-an-xml-object","errorCode":null,"errorMessage":"Value must be an XML object.","messagePattern":"Value must be an XML object\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs","lineNumber":1030,"sourceCode":"            }\n        }\n\n        private IXmlNode WrapXml(object value)\n        {\n#if HAVE_XLINQ\n            if (value is XObject xObject)\n            {\n                return XContainerWrapper.WrapNode(xObject);\n            }\n#endif\n#if HAVE_XML_DOCUMENT\n            if (value is XmlNode node)\n            {\n                return XmlNodeWrapper.WrapNode(node);\n            }\n#endif\n\n            throw new ArgumentException(\"Value must be an XML object.\", nameof(value));\n        }\n\n        private void PushParentNamespaces(IXmlNode node, XmlNamespaceManager manager)\n        {\n            List<IXmlNode>? parentElements = null;\n\n            IXmlNode? parent = node;\n            while ((parent = parent.ParentNode) != null)\n            {\n                if (parent.NodeType == XmlNodeType.Element)\n                {\n                    if (parentElements == null)\n                    {\n                        parentElements = new List<IXmlNode>();\n                    }\n\n                    parentElements.Add(parent);\n                }","sourceCodeStart":1012,"sourceCodeEnd":1048,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs#L1012-L1048","documentation":"Thrown by XmlNodeConverter.WrapXml when the value passed for serialization is neither an XObject (System.Xml.Linq) nor an XmlNode (System.Xml). The converter can only transform XML DOM objects to/from JSON; any other runtime type triggers this ArgumentException.","triggerScenarios":"Serializing a non-XML object through XmlNodeConverter, e.g. applying [JsonConverter(typeof(XmlNodeConverter))] to a POCO, a string, or an XElement-wrapping custom type that does not inherit from XObject/XmlNode.","commonSituations":"Misapplying XmlNodeConverter to a business object instead of an XmlDocument/XDocument. A refactor changed the property type away from XML while the converter attribute remained. Polymorphic values where the actual instance is a plain object.","solutions":["Pass only XmlDocument, XmlElement, XDocument, or XElement values to XmlNodeConverter.","Remove the XmlNodeConverter attribute from non-XML properties.","If you need JSON<->object conversion (not JSON<->XML), use the default serializer or a different converter."],"exampleFix":"// before: converter applied to a non-XML object\n[JsonConverter(typeof(XmlNodeConverter))]\npublic MyPoco Config { get; set; }\n\n// after: serialize XML document directly\nvar doc = new XmlDocument(); doc.LoadXml(xml);\nstring json = JsonConvert.SerializeXmlNode(doc);","handlingStrategy":"type-guard","validationCode":"object value = GetValue();\nif (value != null && !(value is System.Xml.XmlNode) && !(value is System.Xml.Linq.XObject))\n    throw new InvalidOperationException($\"XmlNodeConverter requires an XML object, got {value.GetType()}\");","typeGuard":"static bool IsXmlObject(object v) =>\n    v is System.Xml.XmlNode || v is System.Xml.Linq.XObject;","tryCatchPattern":"try { conv.WriteJson(writer, value, serializer); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"must be an XML object\"))\n{\n    throw new InvalidOperationException($\"XmlNodeConverter got {value?.GetType()}\", ex);\n}","preventionTips":["Only pass XmlDocument/XmlElement/XDocument/XElement to XmlNodeConverter.","Use JsonConvert.SerializeXmlNode/SerializeXNode helper methods which enforce the type at the API boundary.","Do not attach the converter to non-XML POCO properties."],"tags":["serialization","converter","xml","type-mismatch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}