-Fixed converting JSON to XML with invalid XML name characters
This commit is contained in:
+2
-1
@@ -14,4 +14,5 @@ _ReSharper.*
|
||||
.vs/
|
||||
*.lock.json
|
||||
*.nuget.props
|
||||
*.nuget.targets
|
||||
*.nuget.targets
|
||||
*.orig
|
||||
@@ -592,6 +592,34 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}", jsonText);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeNodeTypes_Encoding()
|
||||
{
|
||||
XmlNode node = DeserializeXmlNode(@"{
|
||||
""xs!:Choice!"": {
|
||||
""@msdata:IsDataSet!"": """",
|
||||
""@xmlns:xs!"": ""http://www.w3.org/2001/XMLSchema"",
|
||||
""@xmlns:msdata"": ""urn:schemas-microsoft-com:xml-msdata"",
|
||||
""?xml-stylesheet"": ""href=\""classic.xsl\"" type=\""text/xml\"""",
|
||||
""#cdata-section"": ""<Kiwi>true</Kiwi>""
|
||||
}
|
||||
}");
|
||||
|
||||
Assert.AreEqual(@"<xs_x0021_:Choice_x0021_ msdata:IsDataSet_x0021_="""" xmlns:xs_x0021_=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""><?xml-stylesheet href=""classic.xsl"" type=""text/xml""?><![CDATA[<Kiwi>true</Kiwi>]]></xs_x0021_:Choice_x0021_>", node.InnerXml);
|
||||
|
||||
string json = SerializeXmlNode(node);
|
||||
|
||||
StringAssert.AreEqual(@"{
|
||||
""xs!:Choice!"": {
|
||||
""@msdata:IsDataSet!"": """",
|
||||
""@xmlns:xs!"": ""http://www.w3.org/2001/XMLSchema"",
|
||||
""@xmlns:msdata"": ""urn:schemas-microsoft-com:xml-msdata"",
|
||||
""?xml-stylesheet"": ""href=\""classic.xsl\"" type=\""text/xml\"""",
|
||||
""#cdata-section"": ""<Kiwi>true</Kiwi>""
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DocumentFragmentSerialize()
|
||||
{
|
||||
@@ -1418,6 +1446,50 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
StringAssert.AreEqual(@"<?xml version=""1.0"" standalone=""no""?><root><person id=""1""><name>Alan</name><url>http://www.google.com</url></person><person id=""2""><name>Louis</name><url>http://www.yahoo.com</url></person></root>", doc.InnerXml);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EscapingNames()
|
||||
{
|
||||
string json = @"{
|
||||
""root!"": {
|
||||
""person!"": [
|
||||
{
|
||||
""@id!"": ""1"",
|
||||
""name!"": ""Alan"",
|
||||
""url!"": ""http://www.google.com""
|
||||
},
|
||||
{
|
||||
""@id!"": ""2"",
|
||||
""name!"": ""Louis"",
|
||||
""url!"": ""http://www.yahoo.com""
|
||||
}
|
||||
]
|
||||
}
|
||||
}";
|
||||
|
||||
XmlDocument doc = (XmlDocument)DeserializeXmlNode(json);
|
||||
|
||||
Assert.AreEqual(@"<root_x0021_><person_x0021_ id_x0021_=""1""><name_x0021_>Alan</name_x0021_><url_x0021_>http://www.google.com</url_x0021_></person_x0021_><person_x0021_ id_x0021_=""2""><name_x0021_>Louis</name_x0021_><url_x0021_>http://www.yahoo.com</url_x0021_></person_x0021_></root_x0021_>", doc.InnerXml);
|
||||
|
||||
string json2 = SerializeXmlNode(doc);
|
||||
|
||||
StringAssert.AreEqual(@"{
|
||||
""root!"": {
|
||||
""person!"": [
|
||||
{
|
||||
""@id!"": ""1"",
|
||||
""name!"": ""Alan"",
|
||||
""url!"": ""http://www.google.com""
|
||||
},
|
||||
{
|
||||
""@id!"": ""2"",
|
||||
""name!"": ""Louis"",
|
||||
""url!"": ""http://www.yahoo.com""
|
||||
}
|
||||
]
|
||||
}
|
||||
}", json2);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeDeserializeMetadataProperties()
|
||||
{
|
||||
|
||||
@@ -101,7 +101,7 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
public IXmlElement CreateElement(string elementName)
|
||||
{
|
||||
return new XmlElementWrapper(_document.CreateElement(XmlConvert.EncodeName(elementName)));
|
||||
return new XmlElementWrapper(_document.CreateElement(elementName));
|
||||
}
|
||||
|
||||
public IXmlElement CreateElement(string qualifiedName, string namespaceUri)
|
||||
@@ -1175,7 +1175,7 @@ namespace Newtonsoft.Json.Converters
|
||||
if (attribute.NamespaceUri == "http://www.w3.org/2000/xmlns/")
|
||||
{
|
||||
string namespacePrefix = (attribute.LocalName != "xmlns")
|
||||
? attribute.LocalName
|
||||
? XmlConvert.DecodeName(attribute.LocalName)
|
||||
: string.Empty;
|
||||
string namespaceUri = attribute.Value;
|
||||
|
||||
@@ -1490,11 +1490,12 @@ namespace Newtonsoft.Json.Converters
|
||||
// add attributes to newly created element
|
||||
foreach (KeyValuePair<string, string> nameValue in attributeNameValues)
|
||||
{
|
||||
string encodedName = XmlConvert.EncodeName(nameValue.Key);
|
||||
string attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key);
|
||||
|
||||
IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix))
|
||||
? document.CreateAttribute(nameValue.Key, manager.LookupNamespace(attributePrefix) ?? string.Empty, nameValue.Value)
|
||||
: document.CreateAttribute(nameValue.Key, nameValue.Value);
|
||||
? document.CreateAttribute(encodedName, manager.LookupNamespace(attributePrefix) ?? string.Empty, nameValue.Value)
|
||||
: document.CreateAttribute(encodedName, nameValue.Value);
|
||||
|
||||
element.SetAttributeNode(attribute);
|
||||
}
|
||||
@@ -1531,11 +1532,12 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
private static void AddAttribute(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string attributeName, XmlNamespaceManager manager, string attributePrefix)
|
||||
{
|
||||
string encodedName = XmlConvert.EncodeName(attributeName);
|
||||
string attributeValue = reader.Value.ToString();
|
||||
|
||||
IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix))
|
||||
? document.CreateAttribute(attributeName, manager.LookupNamespace(attributePrefix), attributeValue)
|
||||
: document.CreateAttribute(attributeName, attributeValue);
|
||||
? document.CreateAttribute(encodedName, manager.LookupNamespace(attributePrefix), attributeValue)
|
||||
: document.CreateAttribute(encodedName, attributeValue);
|
||||
|
||||
((IXmlElement)currentNode).SetAttributeNode(attribute);
|
||||
}
|
||||
@@ -1822,9 +1824,10 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
private IXmlElement CreateElement(string elementName, IXmlDocument document, string elementPrefix, XmlNamespaceManager manager)
|
||||
{
|
||||
string encodeName = XmlConvert.EncodeName(elementName);
|
||||
string ns = string.IsNullOrEmpty(elementPrefix) ? manager.DefaultNamespace : manager.LookupNamespace(elementPrefix);
|
||||
|
||||
IXmlElement element = (!string.IsNullOrEmpty(ns)) ? document.CreateElement(elementName, ns) : document.CreateElement(elementName);
|
||||
IXmlElement element = (!string.IsNullOrEmpty(ns)) ? document.CreateElement(encodeName, ns) : document.CreateElement(encodeName);
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user