1

Closed

Namespace prefix leak when serializing xml

description

If a node in the xml has a default namespace, and a child node re-declares the namespace but with a prefix, the nodes following the prefixed node will have the prefix but without the namespace definition.

Example:
var xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<root xmlns=""http://www.example.com/ns"">
  <a/>
  <bns:b xmlns:bns=""http://www.example.com/ns""/>
  <c/>
</root>";

var xml = XElement.Parse(xmlString);

var json = JsonConvert.SerializeObject(xml);
This results in the following json:
{
    "root": {
        "@xmlns": "http://www.example.com/ns",
        "a": null,
        "bns:b": {
            "@xmlns:bns": "http://www.example.com/ns"
        },
        "bns:c": null
    }
}
Note how the c-child now has the "bns" prefix, but without the namespace declaration, making a conversion back to xml impossible (since the xml would be invalid).

Regards,
Espen
Closed Mar 6, 2013 at 6:47 AM by JamesNK
Done

comments

espenwa wrote Mar 6, 2013 at 9:25 AM

Great!

How often do you commit to GitHub, when may I expect it to appear in the source?

-e