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

Fixed XElement serialization with explicit empty namespace. #2685

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,19 @@ public void SerializeXElement()
]", json);
}

[Test]
public void SerializeXElementWithExplicitEmptyNamespace()
{
XNamespace ns = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
var xml = new XElement(ns + "root", new XElement("other", "value"));
// <root xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><other xmlns="">value</other></root>

var xmlAsJson = JsonConvert.SerializeObject(xml);
var deserializedXml = JsonConvert.DeserializeObject<XElement>(xmlAsJson);

Assert.AreEqual(xml.ToString(), deserializedXml.ToString());
}

public class DecimalContainer
{
public decimal Number { get; set; }
Expand Down
28 changes: 10 additions & 18 deletions Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -860,16 +860,21 @@ public override List<IXmlNode> Attributes
}
else
{
bool namespaceAdded = false;
_attributes = new List<IXmlNode>();
foreach (XAttribute attribute in Element.Attributes())
{
if (attribute.Name == "xmlns")
{
namespaceAdded = true;
}
_attributes.Add(new XAttributeWrapper(attribute));
}

// ensure elements created with a namespace but no namespace attribute are converted correctly
// e.g. new XElement("{http://example.com}MyElement");
string namespaceUri = NamespaceUri!;
if (HasImplicitNamespaceAttribute(namespaceUri))
if (!namespaceAdded && HasImplicitNamespaceAttribute(namespaceUri))
{
_attributes.Insert(0, new XAttributeWrapper(new XAttribute("xmlns", namespaceUri)));
}
Expand All @@ -886,24 +891,11 @@ private bool HasImplicitNamespaceAttribute(string namespaceUri)
{
if (StringUtils.IsNullOrEmpty(GetPrefixOfNamespace(namespaceUri)))
{
bool namespaceDeclared = false;

if (Element.HasAttributes)
{
foreach (XAttribute attribute in Element.Attributes())
{
if (attribute.Name.LocalName == "xmlns" && StringUtils.IsNullOrEmpty(attribute.Name.NamespaceName) && attribute.Value == namespaceUri)
{
namespaceDeclared = true;
}
}
}

if (!namespaceDeclared)
{
return true;
}
return true;
}
} else if (ParentNode?.NamespaceUri != null && ParentNode?.NamespaceUri != namespaceUri)
{
return true;
}

return false;
Expand Down