Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't deserialize XML with trailing whitespace #3004

Open
Wdestroier opened this issue Dec 5, 2024 · 0 comments
Open

Can't deserialize XML with trailing whitespace #3004

Wdestroier opened this issue Dec 5, 2024 · 0 comments

Comments

@Wdestroier
Copy link

Wdestroier commented Dec 5, 2024

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant