JamesNK/Newtonsoft.Json

Can't deserialize XML with trailing whitespace

Opened this issue · 0 comments

Source/destination types

var expected = new System.Xml.XmlDocument();

var element = expected.CreateElement("someTag");
var whitespace = expected.CreateWhitespace("\n");

expected.AppendChild(element);
expected.AppendChild(whitespace);

Source/destination JSON

var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.All;

var json = JsonConvert.SerializeObject(expected, settings); // Ok, "{\"nfeProc\":null,\"#whitespace\":\"\\n\"}"

Expected behavior

var actual = JsonConvert.DeserializeObject<XmlDocument>(json, settings);
Assert.AreEqual(expected.OuterXml, actual.OuterXml); // Ok

Actual behavior

var actual = JsonConvert.DeserializeObject<XmlDocument>(json, settings); // Error, JsonSerializationException
Assert.AreEqual(expected.OuterXml, actual.OuterXml);
  • Newtonsoft.Json.JsonSerializationException: JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName. Path '#whitespace', line 1, position 30.