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

JsonConvert.SerializeXNode: Global namespace on child nodes are lost and implicitly replaced with parent elements namespace #2681

Open
tkvalvik opened this issue May 20, 2022 · 0 comments · May be fixed by #2685

Comments

@tkvalvik
Copy link

tkvalvik commented May 20, 2022

Empty namespaces are lost during serialization if parent-element has a non-empty namespace.

Source/destination types

var xml = new XElement("{x}a", new XElement("b", ""));
var json = JsonConvert.SerializeXNode(xml);
Console.WriteLine(json); 

Source/destination JSON

{
  "a": {
    "@xmlns": "x",
    "b": ""
  }
}

Expected behavior

xmlns should be present on an element when it differs from its parent, even when it is the global namespace.

{
  "a": {
    "@xmlns": "x",
    "b": {
      "@xmlns": ""
    }
  }
}

Actual behavior

No xmlns-attribute on the b-element, even though its namespace differs its parent.

This also means that empty namespaces are dropped in a serialize/deserialize round trip.

Steps to reproduce

var xml = new XElement("{x}a", new XElement("b", ""));
Console.WriteLine($"Before: \n:{xml}"); 
var json = JsonConvert.SerializeXNode(xml);
var deserializedXml = JsonConvert.DeserializeXNode(json);
Console.WriteLine($"After: \n:{deserializedXml}");
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

Successfully merging a pull request may close this issue.

1 participant