2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
26
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
28
using System.Collections.Generic;
29
using Newtonsoft.Json.Tests.Serialization;
30
using Newtonsoft.Json.Tests.TestObjects;
32
using NUnit.Framework;
34
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
35
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
36
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
38
using Newtonsoft.Json;
41
using Newtonsoft.Json.Converters;
42
using Newtonsoft.Json.Utilities;
43
using Newtonsoft.Json.Linq;
45
using System.Xml.Linq;
48
namespace Newtonsoft.Json.Tests.Converters
51
public class XmlNodeConverterTest : TestFixtureBase
53
private string SerializeXmlNode(XmlNode node)
55
string json = JsonConvert.SerializeXmlNode(node, Formatting.Indented);
56
XmlNodeReader reader = new XmlNodeReader(node);
60
if (node is XmlDocument)
62
xNode = XDocument.Load(reader);
64
else if (node is XmlAttribute)
66
XmlAttribute attribute = (XmlAttribute) node;
67
xNode = new XAttribute(XName.Get(attribute.LocalName, attribute.NamespaceURI), attribute.Value);
71
reader.MoveToContent();
72
xNode = XNode.ReadFrom(reader);
75
string linqJson = JsonConvert.SerializeXNode(xNode, Formatting.Indented);
77
Assert.AreEqual(json, linqJson);
83
private XmlNode DeserializeXmlNode(string json)
85
return DeserializeXmlNode(json, null);
88
private XmlNode DeserializeXmlNode(string json, string deserializeRootElementName)
90
JsonTextReader reader;
92
reader = new JsonTextReader(new StringReader(json));
94
XmlNodeConverter converter = new XmlNodeConverter();
95
if (deserializeRootElementName != null)
96
converter.DeserializeRootElementName = deserializeRootElementName;
98
XmlNode node = (XmlNode)converter.ReadJson(reader, typeof (XmlDocument), null, new JsonSerializer());
101
string xmlText = node.OuterXml;
103
reader = new JsonTextReader(new StringReader(json));
105
XDocument d = (XDocument) converter.ReadJson(reader, typeof (XDocument), null, new JsonSerializer());
107
string linqXmlText = d.ToString(SaveOptions.DisableFormatting);
108
if (d.Declaration != null)
109
linqXmlText = d.Declaration + linqXmlText;
111
Assert.AreEqual(xmlText, linqXmlText);
119
public void SerializeEmptyDocument()
121
XmlDocument doc = new XmlDocument();
122
doc.LoadXml("<root />");
124
string json = JsonConvert.SerializeXmlNode(doc, Formatting.Indented, true);
125
Assert.AreEqual("null", json);
127
doc = new XmlDocument();
128
doc.LoadXml("<root></root>");
130
json = JsonConvert.SerializeXmlNode(doc, Formatting.Indented, true);
131
Assert.AreEqual("null", json);
134
XDocument doc1 = XDocument.Parse("<root />");
136
json = JsonConvert.SerializeXNode(doc1, Formatting.Indented, true);
137
Assert.AreEqual("null", json);
139
doc1 = XDocument.Parse("<root></root>");
141
json = JsonConvert.SerializeXNode(doc1, Formatting.Indented, true);
142
Assert.AreEqual("null", json);
147
public void DocumentSerializeIndented()
149
string xml = @"<?xml version=""1.0"" standalone=""no""?>
150
<?xml-stylesheet href=""classic.xsl"" type=""text/xml""?>
151
<span class=""vevent"">
152
<a class=""url"" href=""http://www.web2con.com/"">
153
<span class=""summary"">Web 2.0 Conference<![CDATA[my escaped text]]></span>
154
<abbr class=""dtstart"" title=""2005-10-05"">October 5</abbr>
155
<abbr class=""dtend"" title=""2005-10-08"">7</abbr>
156
<span class=""location"">Argent Hotel, San Francisco, CA</span>
159
XmlDocument doc = new XmlDocument();
162
string jsonText = SerializeXmlNode(doc);
163
string expected = @"{
165
""@version"": ""1.0"",
166
""@standalone"": ""no""
168
""?xml-stylesheet"": ""href=\""classic.xsl\"" type=\""text/xml\"""",
170
""@class"": ""vevent"",
173
""@href"": ""http://www.web2con.com/"",
176
""@class"": ""summary"",
177
""#text"": ""Web 2.0 Conference"",
178
""#cdata-section"": ""my escaped text""
181
""@class"": ""location"",
182
""#text"": ""Argent Hotel, San Francisco, CA""
187
""@class"": ""dtstart"",
188
""@title"": ""2005-10-05"",
189
""#text"": ""October 5""
192
""@class"": ""dtend"",
193
""@title"": ""2005-10-08"",
201
Assert.AreEqual(expected, jsonText);
203
Console.WriteLine("DocumentSerializeIndented");
204
Console.WriteLine(jsonText);
209
public void SerializeNodeTypes()
211
XmlDocument doc = new XmlDocument();
214
Console.WriteLine("SerializeNodeTypes");
216
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
217
<xs:schema xs:id=""SomeID""
219
xmlns:xs=""http://www.w3.org/2001/XMLSchema""
220
xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
221
<xs:element name=""MyDataSet"" msdata:IsDataSet=""true"">
225
XmlDocument document = new XmlDocument();
226
document.LoadXml(xml);
229
XmlAttribute attribute = document.DocumentElement.ChildNodes[0].Attributes["IsDataSet", "urn:schemas-microsoft-com:xml-msdata"];
230
attribute.Value = "true";
232
jsonText = JsonConvert.SerializeXmlNode(attribute);
234
Console.WriteLine(jsonText);
235
Assert.AreEqual(@"{""@msdata:IsDataSet"":""true""}", jsonText);
238
XDocument d = XDocument.Parse(xml);
239
XAttribute a = d.Root.Element("{http://www.w3.org/2001/XMLSchema}element").Attribute("{urn:schemas-microsoft-com:xml-msdata}IsDataSet");
241
jsonText = JsonConvert.SerializeXNode(a);
243
Assert.AreEqual(@"{""@msdata:IsDataSet"":""true""}", jsonText);
246
// XmlProcessingInstruction
247
XmlProcessingInstruction instruction = doc.CreateProcessingInstruction("xml-stylesheet", @"href=""classic.xsl"" type=""text/xml""");
249
jsonText = JsonConvert.SerializeXmlNode(instruction);
251
Console.WriteLine(jsonText);
252
Assert.AreEqual(@"{""?xml-stylesheet"":""href=\""classic.xsl\"" type=\""text/xml\""""}", jsonText);
255
// XmlProcessingInstruction
256
XmlCDataSection cDataSection = doc.CreateCDataSection("<Kiwi>true</Kiwi>");
258
jsonText = JsonConvert.SerializeXmlNode(cDataSection);
260
Console.WriteLine(jsonText);
261
Assert.AreEqual(@"{""#cdata-section"":""<Kiwi>true</Kiwi>""}", jsonText);
265
XmlElement element = doc.CreateElement("xs", "Choice", "http://www.w3.org/2001/XMLSchema");
266
element.SetAttributeNode(doc.CreateAttribute("msdata", "IsDataSet", "urn:schemas-microsoft-com:xml-msdata"));
268
XmlAttribute aa = doc.CreateAttribute(@"xmlns", "xs", "http://www.w3.org/2000/xmlns/");
269
aa.Value = "http://www.w3.org/2001/XMLSchema";
270
element.SetAttributeNode(aa);
272
aa = doc.CreateAttribute(@"xmlns", "msdata", "http://www.w3.org/2000/xmlns/");
273
aa.Value = "urn:schemas-microsoft-com:xml-msdata";
274
element.SetAttributeNode(aa);
276
element.AppendChild(instruction);
277
element.AppendChild(cDataSection);
279
doc.AppendChild(element);
281
jsonText = JsonConvert.SerializeXmlNode(element, Formatting.Indented);
285
""@msdata:IsDataSet"": """",
286
""@xmlns:xs"": ""http://www.w3.org/2001/XMLSchema"",
287
""@xmlns:msdata"": ""urn:schemas-microsoft-com:xml-msdata"",
288
""?xml-stylesheet"": ""href=\""classic.xsl\"" type=\""text/xml\"""",
289
""#cdata-section"": ""<Kiwi>true</Kiwi>""
295
public void DocumentFragmentSerialize()
297
XmlDocument doc = new XmlDocument();
299
XmlDocumentFragment fragement = doc.CreateDocumentFragment();
301
fragement.InnerXml = "<Item>widget</Item><Item>widget</Item>";
303
string jsonText = JsonConvert.SerializeXmlNode(fragement);
305
string expected = @"{""Item"":[""widget"",""widget""]}";
307
Assert.AreEqual(expected, jsonText);
309
Console.WriteLine("DocumentFragmentSerialize");
310
Console.WriteLine(jsonText);
315
public void NamespaceSerializeDeserialize()
317
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
318
<xs:schema xs:id=""SomeID""
320
xmlns:xs=""http://www.w3.org/2001/XMLSchema""
321
xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
322
<xs:element name=""MyDataSet"" msdata:IsDataSet=""true"">
324
<xs:choice maxOccurs=""unbounded"">
325
<xs:element name=""customers"" >
328
<xs:element name=""CustomerID"" type=""xs:integer""
330
<xs:element name=""CompanyName"" type=""xs:string""
332
<xs:element name=""Phone"" type=""xs:string"" />
341
XmlDocument doc = new XmlDocument();
344
string jsonText = SerializeXmlNode(doc);
346
string expected = @"{
348
""@version"": ""1.0"",
349
""@encoding"": ""utf-8""
352
""@xs:id"": ""SomeID"",
354
""@xmlns:xs"": ""http://www.w3.org/2001/XMLSchema"",
355
""@xmlns:msdata"": ""urn:schemas-microsoft-com:xml-msdata"",
357
""@name"": ""MyDataSet"",
358
""@msdata:IsDataSet"": ""true"",
359
""xs:complexType"": {
361
""@maxOccurs"": ""unbounded"",
363
""@name"": ""customers"",
364
""xs:complexType"": {
368
""@name"": ""CustomerID"",
369
""@type"": ""xs:integer"",
370
""@minOccurs"": ""0""
373
""@name"": ""CompanyName"",
374
""@type"": ""xs:string"",
375
""@minOccurs"": ""0""
378
""@name"": ""Phone"",
379
""@type"": ""xs:string""
391
Assert.AreEqual(expected, jsonText);
393
XmlDocument deserializedDoc = (XmlDocument)DeserializeXmlNode(jsonText);
395
Assert.AreEqual(doc.InnerXml, deserializedDoc.InnerXml);
397
Console.WriteLine("NamespaceSerializeDeserialize");
398
Console.WriteLine(jsonText);
399
Console.WriteLine(deserializedDoc.InnerXml);
404
public void DocumentDeserialize()
406
string jsonText = @"{
408
""@version"": ""1.0"",
409
""@standalone"": ""no""
412
""@class"": ""vevent"",
416
""@class"": ""summary"",
417
""#text"": ""Web 2.0 Conference"",
418
""#cdata-section"": ""my escaped text""
420
""@href"": ""http://www.web2con.com/""
425
XmlDocument doc = (XmlDocument)DeserializeXmlNode(jsonText);
427
string expected = @"<?xml version=""1.0"" standalone=""no""?>
428
<span class=""vevent"">
429
<a class=""url"" href=""http://www.web2con.com/"">
430
<span class=""summary"">Web 2.0 Conference<![CDATA[my escaped text]]></span>
434
string formattedXml = GetIndentedInnerXml(doc);
436
Console.WriteLine("DocumentDeserialize");
437
Console.WriteLine(formattedXml);
440
Assert.AreEqual(expected, formattedXml);
443
private string GetIndentedInnerXml(XmlNode node)
445
XmlWriterSettings settings = new XmlWriterSettings();
446
settings.Indent = true;
448
StringWriter sw = new StringWriter();
450
using (XmlWriter writer = XmlWriter.Create(sw, settings))
452
node.WriteTo(writer);
455
return sw.ToString();
459
public void SingleTextNode()
461
string xml = @"<?xml version=""1.0"" standalone=""no""?>
465
<url>http://www.google.com</url>
469
<url>http://www.yahoo.com</url>
473
XmlDocument doc = new XmlDocument();
476
string jsonText = SerializeXmlNode(doc);
478
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
480
Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);
484
public void EmptyNode()
486
string xml = @"<?xml version=""1.0"" standalone=""no""?>
494
<url>http://www.yahoo.com</url>
498
XmlDocument doc = new XmlDocument();
501
string jsonText = SerializeXmlNode(doc);
503
Console.WriteLine(jsonText);
505
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
507
Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);
511
public void OtherElementDataTypes()
513
string jsonText = @"{""?xml"":{""@version"":""1.0"",""@standalone"":""no""},""root"":{""person"":[{""@id"":""1"",""Float"":2.5,""Integer"":99},{""Boolean"":true,""@id"":""2"",""date"":""\/Date(954374400000)\/""}]}}";
515
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
517
string expected = @"<?xml version=""1.0"" standalone=""no""?><root><person id=""1""><Float>2.5</Float><Integer>99</Integer></person><person id=""2""><Boolean>true</Boolean><date>2000-03-30T00:00:00Z</date></person></root>";
519
Assert.AreEqual(expected, newDoc.InnerXml);
523
public void NoRootObject()
525
ExceptionAssert.Throws<JsonSerializationException>(
526
"XmlNodeConverter can only convert JSON that begins with an object.",
529
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"[1]");
534
public void RootObjectMultipleProperties()
536
ExceptionAssert.Throws<JsonSerializationException>(
537
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.",
540
XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(@"{Prop1:1,Prop2:2}");
545
public void JavaScriptConstructor()
547
string jsonText = @"{root:{r:new Date(34343, 55)}}";
549
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
551
string expected = @"<root><r><Date>34343</Date><Date>55</Date></r></root>";
553
Assert.AreEqual(expected, newDoc.InnerXml);
555
string json = SerializeXmlNode(newDoc);
567
Assert.AreEqual(expected, json);
571
public void ForceJsonArray()
573
string arrayXml = @"<root xmlns:json=""http://james.newtonking.com/projects/json"">
576
<url>http://www.google.com</url>
577
<role json:Array=""true"">Admin</role>
581
XmlDocument arrayDoc = new XmlDocument();
582
arrayDoc.LoadXml(arrayXml);
584
string arrayJsonText = SerializeXmlNode(arrayDoc);
585
string expected = @"{
590
""url"": ""http://www.google.com"",
597
Assert.AreEqual(expected, arrayJsonText);
599
arrayXml = @"<root xmlns:json=""http://james.newtonking.com/projects/json"">
602
<url>http://www.google.com</url>
603
<role json:Array=""true"">Admin1</role>
604
<role json:Array=""true"">Admin2</role>
608
arrayDoc = new XmlDocument();
609
arrayDoc.LoadXml(arrayXml);
611
arrayJsonText = SerializeXmlNode(arrayDoc);
617
""url"": ""http://www.google.com"",
625
Assert.AreEqual(expected, arrayJsonText);
627
arrayXml = @"<root xmlns:json=""http://james.newtonking.com/projects/json"">
630
<url>http://www.google.com</url>
631
<role json:Array=""false"">Admin1</role>
635
arrayDoc = new XmlDocument();
636
arrayDoc.LoadXml(arrayXml);
638
arrayJsonText = SerializeXmlNode(arrayDoc);
644
""url"": ""http://www.google.com"",
649
Assert.AreEqual(expected, arrayJsonText);
653
public void MultipleRootPropertiesXmlDocument()
655
string json = @"{""count"": 773840,""photos"": null}";
657
ExceptionAssert.Throws<JsonSerializationException>(
658
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.",
661
JsonConvert.DeserializeXmlNode(json);
667
public void MultipleRootPropertiesXDocument()
669
string json = @"{""count"": 773840,""photos"": null}";
671
ExceptionAssert.Throws<JsonSerializationException>(
672
"JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.",
675
JsonConvert.DeserializeXNode(json);
681
public void MultipleRootPropertiesAddRootElement()
683
string json = @"{""count"": 773840,""photos"": 773840}";
685
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot");
687
Assert.AreEqual(@"<myRoot><count>773840</count><photos>773840</photos></myRoot>", newDoc.InnerXml);
690
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
692
Assert.AreEqual(@"<myRoot><count>773840</count><photos>773840</photos></myRoot>", newXDoc.ToString(SaveOptions.DisableFormatting));
697
public void NestedArrays()
700
""available_sizes"": [
702
""assets/images/resized/0001/1070/11070v1-max-150x150.jpg"",
703
""assets/images/resized/0001/1070/11070v1-max-150x150.jpg""
706
""assets/images/resized/0001/1070/11070v1-max-250x250.jpg"",
707
""assets/images/resized/0001/1070/11070v1-max-250x250.jpg""
710
""assets/images/resized/0001/1070/11070v1-max-250x250.jpg""
715
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot");
717
string xml = IndentXml(newDoc.InnerXml);
719
Assert.AreEqual(@"<myRoot>
721
<available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes>
722
<available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes>
725
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
726
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
729
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
731
</myRoot>", IndentXml(newDoc.InnerXml));
734
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
736
Assert.AreEqual(@"<myRoot>
738
<available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes>
739
<available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes>
742
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
743
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
746
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
748
</myRoot>", IndentXml(newXDoc.ToString(SaveOptions.DisableFormatting)));
751
string newJson = JsonConvert.SerializeXmlNode(newDoc, Formatting.Indented);
752
Console.WriteLine(newJson);
756
public void RoundTripNestedArrays()
759
""available_sizes"": [
761
""assets/images/resized/0001/1070/11070v1-max-150x150.jpg"",
762
""assets/images/resized/0001/1070/11070v1-max-150x150.jpg""
765
""assets/images/resized/0001/1070/11070v1-max-250x250.jpg"",
766
""assets/images/resized/0001/1070/11070v1-max-250x250.jpg""
769
""assets/images/resized/0001/1070/11070v1-max-250x250.jpg""
774
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot", true);
776
Assert.AreEqual(@"<myRoot>
777
<available_sizes json:Array=""true"" xmlns:json=""http://james.newtonking.com/projects/json"">
778
<available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes>
779
<available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes>
781
<available_sizes json:Array=""true"" xmlns:json=""http://james.newtonking.com/projects/json"">
782
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
783
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
785
<available_sizes json:Array=""true"" xmlns:json=""http://james.newtonking.com/projects/json"">
786
<available_sizes json:Array=""true"">assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
788
</myRoot>", IndentXml(newDoc.InnerXml));
791
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot", true);
793
Console.WriteLine(IndentXml(newXDoc.ToString(SaveOptions.DisableFormatting)));
795
Assert.AreEqual(@"<myRoot>
796
<available_sizes json:Array=""true"" xmlns:json=""http://james.newtonking.com/projects/json"">
797
<available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes>
798
<available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes>
800
<available_sizes json:Array=""true"" xmlns:json=""http://james.newtonking.com/projects/json"">
801
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
802
<available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
804
<available_sizes json:Array=""true"" xmlns:json=""http://james.newtonking.com/projects/json"">
805
<available_sizes json:Array=""true"">assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes>
807
</myRoot>", IndentXml(newXDoc.ToString(SaveOptions.DisableFormatting)));
810
string newJson = JsonConvert.SerializeXmlNode(newDoc, Formatting.Indented, true);
811
Assert.AreEqual(json, newJson);
815
public void MultipleNestedArraysToXml()
818
""available_sizes"": [
821
""assets/images/resized/0001/1070/11070v1-max-150x150.jpg""
825
""assets/images/resized/0001/1070/11070v1-max-250x250.jpg""
829
""assets/images/resized/0001/1070/11070v1-max-450x450.jpg""
834
XmlDocument newDoc = JsonConvert.DeserializeXmlNode(json, "myRoot");
836
Assert.AreEqual(@"<myRoot><available_sizes><available_sizes><available_sizes>113</available_sizes><available_sizes>150</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes></available_sizes><available_sizes><available_sizes><available_sizes>189</available_sizes><available_sizes>250</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes></available_sizes><available_sizes><available_sizes><available_sizes>341</available_sizes><available_sizes>450</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-450x450.jpg</available_sizes></available_sizes></myRoot>", newDoc.InnerXml);
839
XDocument newXDoc = JsonConvert.DeserializeXNode(json, "myRoot");
841
Assert.AreEqual(@"<myRoot><available_sizes><available_sizes><available_sizes>113</available_sizes><available_sizes>150</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-150x150.jpg</available_sizes></available_sizes><available_sizes><available_sizes><available_sizes>189</available_sizes><available_sizes>250</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-250x250.jpg</available_sizes></available_sizes><available_sizes><available_sizes><available_sizes>341</available_sizes><available_sizes>450</available_sizes></available_sizes><available_sizes>assets/images/resized/0001/1070/11070v1-max-450x450.jpg</available_sizes></available_sizes></myRoot>", newXDoc.ToString(SaveOptions.DisableFormatting));
846
public void Encoding()
848
XmlDocument doc = new XmlDocument();
850
doc.LoadXml(@"<name>O""Connor</name>"); // i use "" so it will be easier to see the problem
852
string json = SerializeXmlNode(doc);
854
""name"": ""O\""Connor""
859
public void SerializeComment()
861
string xml = @"<span class=""vevent"">
862
<a class=""url"" href=""http://www.web2con.com/"">Text</a><!-- Hi! -->
864
XmlDocument doc = new XmlDocument();
867
string jsonText = SerializeXmlNode(doc);
869
string expected = @"{
871
""@class"": ""vevent"",
874
""@href"": ""http://www.web2con.com/"",
880
Assert.AreEqual(expected, jsonText);
882
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
883
Assert.AreEqual(@"<span class=""vevent""><a class=""url"" href=""http://www.web2con.com/"">Text</a><!-- Hi! --></span>", newDoc.InnerXml);
887
public void SerializeExample()
889
string xml = @"<?xml version=""1.0"" standalone=""no""?>
893
<url>http://www.google.com</url>
897
<url>http://www.yahoo.com</url>
901
XmlDocument doc = new XmlDocument();
904
string jsonText = SerializeXmlNode(doc);
907
// "@version": "1.0",
908
// "@standalone": "no"
915
// "url": "http://www.google.com"
920
// "url": "http://www.yahoo.com"
927
jsonText = JObject.Parse(jsonText).ToString();
931
""@version"": ""1.0"",
932
""@standalone"": ""no""
939
""url"": ""http://www.google.com""
944
""url"": ""http://www.yahoo.com""
950
XmlDocument newDoc = (XmlDocument)DeserializeXmlNode(jsonText);
952
Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);
956
public void DeserializeExample()
960
""@version"": ""1.0"",
961
""@standalone"": ""no""
968
""url"": ""http://www.google.com""
973
""url"": ""http://www.yahoo.com""
979
XmlDocument doc = (XmlDocument)DeserializeXmlNode(json);
980
// <?xml version="1.0" standalone="no"?>
984
// <url>http://www.google.com</url>
987
// <name>Louis</name>
988
// <url>http://www.yahoo.com</url>
992
Assert.AreEqual(@"<?xml version=""1.0"" standalone=""no""?>
996
<url>http://www.google.com</url>
1000
<url>http://www.yahoo.com</url>
1002
</root>".Replace(Environment.NewLine, string.Empty), doc.InnerXml);
1006
public void SerializeDeserializeSpecialProperties()
1008
PreserveReferencesHandlingTests.CircularDictionary circularDictionary = new PreserveReferencesHandlingTests.CircularDictionary();
1009
circularDictionary.Add("other", new PreserveReferencesHandlingTests.CircularDictionary { { "blah", null } });
1010
circularDictionary.Add("self", circularDictionary);
1012
string json = JsonConvert.SerializeObject(circularDictionary, Formatting.Indented,
1013
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.All });
1026
XmlNode node = DeserializeXmlNode(json, "root");
1027
string xml = GetIndentedInnerXml(node);
1028
string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
1029
<root xmlns:json=""http://james.newtonking.com/projects/json"" json:id=""1"">
1030
<other json:id=""2"">
1033
<self json:ref=""1"" />
1036
Assert.AreEqual(expected, xml);
1038
string xmlJson = SerializeXmlNode(node);
1039
string expectedXmlJson = @"{
1052
Assert.AreEqual(expectedXmlJson, xmlJson);
1056
public void EmptyPropertyName()
1062
""ID_EXPIRATION_1"": {
1063
""VALUE"": ""12/12/2000"",
1064
""DATATYPE"": ""D"",
1065
""MSG"": ""Missing Identification Exp. Date 1""
1067
""ID_ISSUEDATE_1"": {
1069
""DATATYPE"": ""D"",
1070
""MSG"": ""Missing Identification Issue Date 1""
1076
""FUNDING_SOURCE"": {
1078
""DATATYPE"": ""L"",
1079
""MSG"": ""Missing Source of Funds""
1085
""34534634535345"": {
1090
""DATATYPE"": ""S"",
1091
""MSG"": ""Missing Picture ID""
1093
""ID_EXPIRATION_1"": {
1094
""VALUE"": ""12/12/2000"",
1095
""DATATYPE"": ""D"",
1096
""MSG"": ""Missing Picture ID""
1100
""DATATYPE"": ""L"",
1101
""MSG"": ""Missing Walk in""
1103
""PERSONAL_MEETING"": {
1105
""DATATYPE"": ""L"",
1106
""MSG"": ""Missing Met Client in Person""
1108
""ID_ISSUEDATE_1"": {
1110
""DATATYPE"": ""D"",
1111
""MSG"": ""Missing Picture ID""
1115
""DATATYPE"": ""L"",
1116
""MSG"": ""Missing Picture ID""
1120
""DATATYPE"": ""L"",
1121
""MSG"": ""Missing Picture ID""
1127
""FUNDING_SOURCE"": {
1129
""DATATYPE"": ""L"",
1130
""MSG"": ""Missing Source of Funds""
1137
ExceptionAssert.Throws<JsonSerializationException>(
1138
"XmlNodeConverter cannot convert JSON with an empty property name to XML.",
1141
DeserializeXmlNode(json);
1146
public void SingleItemArrayPropertySerialization()
1148
Product product = new Product();
1150
product.Name = "Apple";
1151
product.ExpiryDate = new DateTime(2008, 12, 28, 0, 0, 0, DateTimeKind.Utc);
1152
product.Price = 3.99M;
1153
product.Sizes = new string[] { "Small" };
1155
string output = JsonConvert.SerializeObject(product, new IsoDateTimeConverter());
1157
XmlDocument xmlProduct = JsonConvert.DeserializeXmlNode(output, "product", true);
1159
Assert.AreEqual(@"<product>
1161
<ExpiryDate>2008-12-28T00:00:00Z</ExpiryDate>
1163
<Sizes json:Array=""true"" xmlns:json=""http://james.newtonking.com/projects/json"">Small</Sizes>
1164
</product>", IndentXml(xmlProduct.InnerXml));
1166
string output2 = JsonConvert.SerializeXmlNode(xmlProduct.DocumentElement, Formatting.Indented);
1170
""Name"": ""Apple"",
1171
""ExpiryDate"": ""2008-12-28T00:00:00Z"",
1172
""Price"": ""3.99"",
1180
public class TestComplexArrayClass
1182
public string Name { get; set; }
1183
public IList<Product> Products { get; set; }
1187
public void ComplexSingleItemArrayPropertySerialization()
1189
TestComplexArrayClass o = new TestComplexArrayClass
1192
Products = new List<Product>
1194
new Product { Name = "First" }
1198
string output = JsonConvert.SerializeObject(o, new IsoDateTimeConverter());
1200
XmlDocument xmlProduct = JsonConvert.DeserializeXmlNode(output, "test", true);
1202
Assert.AreEqual(@"<test>
1204
<Products json:Array=""true"" xmlns:json=""http://james.newtonking.com/projects/json"">
1206
<ExpiryDate>2000-01-01T00:00:00Z</ExpiryDate>
1210
</test>", IndentXml(xmlProduct.InnerXml));
1212
string output2 = JsonConvert.SerializeXmlNode(xmlProduct.DocumentElement, Formatting.Indented, true);
1218
""Name"": ""First"",
1219
""ExpiryDate"": ""2000-01-01T00:00:00Z"",
1227
private string IndentXml(string xml)
1229
XmlReader reader = XmlReader.Create(new StringReader(xml));
1231
StringWriter sw = new StringWriter();
1232
XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
1234
while (reader.Read())
1236
writer.WriteNode(reader, false);
1241
return sw.ToString();
1245
public void OmitRootObject()
1247
string xml = @"<test>
1250
<Products json:Array=""true"" xmlns:json=""http://james.newtonking.com/projects/json"">
1252
<ExpiryDate>2000-01-01T00:00:00Z</ExpiryDate>
1258
XmlDocument d = new XmlDocument();
1261
string output = JsonConvert.SerializeXmlNode(d, Formatting.Indented, true);
1270
""Name"": ""First"",
1271
""ExpiryDate"": ""2000-01-01T00:00:00Z"",
1280
public void EmtpyElementWithArrayAttributeShouldWriteAttributes()
1282
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
1283
<root xmlns:json=""http://james.newtonking.com/projects/json"">
1285
<B name=""sample"" json:Array=""true""/>
1291
XmlDocument d = new XmlDocument();
1294
string json = JsonConvert.SerializeXmlNode(d, Formatting.Indented);
1298
""@version"": ""1.0"",
1299
""@encoding"": ""utf-8""
1305
""@name"": ""sample""
1318
public void EmtpyElementWithArrayAttributeShouldWriteElement()
1320
string xml = @"<root>
1321
<Reports d1p1:Array=""true"" xmlns:d1p1=""http://james.newtonking.com/projects/json"" />
1324
XmlDocument d = new XmlDocument();
1327
string json = JsonConvert.SerializeXmlNode(d, Formatting.Indented);
1339
public void DeserializeNonInt64IntegerValues()
1341
var dict = new Dictionary<string, object> { { "Int16", (short)1 }, { "Float", 2f }, { "Int32", 3 } };
1342
var obj = JObject.FromObject(dict);
1343
var serializer = JsonSerializer.Create(new JsonSerializerSettings { Converters = { new XmlNodeConverter() { DeserializeRootElementName = "root" } } });
1344
using (var reader = obj.CreateReader())
1346
var value = (XmlDocument)serializer.Deserialize(reader, typeof(XmlDocument));
1348
Assert.AreEqual(@"<root><Int16>1</Int16><Float>2</Float><Int32>3</Int32></root>", value.InnerXml);
1353
public void DeserializingBooleanValues()
1355
MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(@"{root:{""@booleanType"":true}}"));
1356
MemoryStream xml = new MemoryStream();
1358
JsonBodyToSoapXml(ms, xml);
1360
string xmlString = System.Text.Encoding.UTF8.GetString(xml.ToArray());
1362
Assert.AreEqual(@"ļ»æ<?xml version=""1.0"" encoding=""utf-8""?><root booleanType=""true"" />", xmlString);
1365
private static void JsonBodyToSoapXml(Stream json, Stream xml)
1367
Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
1368
settings.Converters.Add(new Newtonsoft.Json.Converters.XmlNodeConverter());
1369
Newtonsoft.Json.JsonSerializer serializer = Newtonsoft.Json.JsonSerializer.Create(settings);
1370
using (Newtonsoft.Json.JsonTextReader reader = new Newtonsoft.Json.JsonTextReader(new System.IO.StreamReader(json)))
1372
XmlDocument doc = (XmlDocument)serializer.Deserialize(reader, typeof(XmlDocument));
1373
if (reader.Read() && reader.TokenType != JsonToken.Comment)
1374
throw new JsonSerializationException("Additional text found in JSON string after finishing deserializing object.");
1375
using (XmlWriter writer = XmlWriter.Create(xml))
1384
public void DeserializeXNodeDefaultNamespace()
1386
string xaml = @"<Grid xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:toolkit=""clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"" Style=""{StaticResource trimFormGrid}"" x:Name=""TrimObjectForm"">
1387
<Grid.ColumnDefinitions>
1388
<ColumnDefinition Width=""63*"" />
1389
<ColumnDefinition Width=""320*"" />
1390
</Grid.ColumnDefinitions>
1391
<Grid.RowDefinitions xmlns="""">
1400
</Grid.RowDefinitions>
1401
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding TypedTitle, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordTypedTitle"" Grid.Column=""1"" Grid.Row=""0"" xmlns="""" />
1402
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding ExternalReference, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordExternalReference"" Grid.Column=""1"" Grid.Row=""1"" xmlns="""" />
1403
<toolkit:DatePicker Style=""{StaticResource trimFormGrid_DP}"" Value=""{Binding DateCreated, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordDateCreated"" Grid.Column=""1"" Grid.Row=""2"" />
1404
<toolkit:DatePicker Style=""{StaticResource trimFormGrid_DP}"" Value=""{Binding DateDue, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordDateDue"" Grid.Column=""1"" Grid.Row=""3"" />
1405
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Author, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordAuthor"" Grid.Column=""1"" Grid.Row=""4"" xmlns="""" />
1406
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Container, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordContainer"" Grid.Column=""1"" Grid.Row=""5"" xmlns="""" />
1407
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding IsEnclosed, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordIsEnclosed"" Grid.Column=""1"" Grid.Row=""6"" xmlns="""" />
1408
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Assignee, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordAssignee"" Grid.Column=""1"" Grid.Row=""7"" xmlns="""" />
1409
<TextBlock Grid.Column=""0"" Text=""Title (Free Text Part)"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""0"" xmlns="""" />
1410
<TextBlock Grid.Column=""0"" Text=""External ID"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""1"" xmlns="""" />
1411
<TextBlock Grid.Column=""0"" Text=""Date Created"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""2"" xmlns="""" />
1412
<TextBlock Grid.Column=""0"" Text=""Date Due"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""3"" xmlns="""" />
1413
<TextBlock Grid.Column=""0"" Text=""Author"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""4"" xmlns="""" />
1414
<TextBlock Grid.Column=""0"" Text=""Container"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""5"" xmlns="""" />
1415
<TextBlock Grid.Column=""0"" Text=""Enclosed?"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""6"" xmlns="""" />
1416
<TextBlock Grid.Column=""0"" Text=""Assignee"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""7"" xmlns="""" />
1419
string json = JsonConvert.SerializeXNode(XDocument.Parse(xaml), Formatting.Indented);
1421
string expectedJson = @"{
1423
""@xmlns"": ""http://schemas.microsoft.com/winfx/2006/xaml/presentation"",
1424
""@xmlns:x"": ""http://schemas.microsoft.com/winfx/2006/xaml"",
1425
""@xmlns:toolkit"": ""clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"",
1426
""@Style"": ""{StaticResource trimFormGrid}"",
1427
""@x:Name"": ""TrimObjectForm"",
1428
""Grid.ColumnDefinitions"": {
1429
""ColumnDefinition"": [
1434
""@Width"": ""320*""
1438
""Grid.RowDefinitions"": {
1440
""RowDefinition"": [
1453
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1454
""@Text"": ""{Binding TypedTitle, Converter={StaticResource trimPropertyConverter}}"",
1455
""@Name"": ""RecordTypedTitle"",
1456
""@Grid.Column"": ""1"",
1457
""@Grid.Row"": ""0"",
1461
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1462
""@Text"": ""{Binding ExternalReference, Converter={StaticResource trimPropertyConverter}}"",
1463
""@Name"": ""RecordExternalReference"",
1464
""@Grid.Column"": ""1"",
1465
""@Grid.Row"": ""1"",
1469
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1470
""@Text"": ""{Binding Author, Converter={StaticResource trimPropertyConverter}}"",
1471
""@Name"": ""RecordAuthor"",
1472
""@Grid.Column"": ""1"",
1473
""@Grid.Row"": ""4"",
1477
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1478
""@Text"": ""{Binding Container, Converter={StaticResource trimPropertyConverter}}"",
1479
""@Name"": ""RecordContainer"",
1480
""@Grid.Column"": ""1"",
1481
""@Grid.Row"": ""5"",
1485
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1486
""@Text"": ""{Binding IsEnclosed, Converter={StaticResource trimPropertyConverter}}"",
1487
""@Name"": ""RecordIsEnclosed"",
1488
""@Grid.Column"": ""1"",
1489
""@Grid.Row"": ""6"",
1493
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1494
""@Text"": ""{Binding Assignee, Converter={StaticResource trimPropertyConverter}}"",
1495
""@Name"": ""RecordAssignee"",
1496
""@Grid.Column"": ""1"",
1497
""@Grid.Row"": ""7"",
1501
""toolkit:DatePicker"": [
1503
""@Style"": ""{StaticResource trimFormGrid_DP}"",
1504
""@Value"": ""{Binding DateCreated, Converter={StaticResource trimPropertyConverter}}"",
1505
""@Name"": ""RecordDateCreated"",
1506
""@Grid.Column"": ""1"",
1507
""@Grid.Row"": ""2""
1510
""@Style"": ""{StaticResource trimFormGrid_DP}"",
1511
""@Value"": ""{Binding DateDue, Converter={StaticResource trimPropertyConverter}}"",
1512
""@Name"": ""RecordDateDue"",
1513
""@Grid.Column"": ""1"",
1514
""@Grid.Row"": ""3""
1519
""@Grid.Column"": ""0"",
1520
""@Text"": ""Title (Free Text Part)"",
1521
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1522
""@Grid.Row"": ""0"",
1526
""@Grid.Column"": ""0"",
1527
""@Text"": ""External ID"",
1528
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1529
""@Grid.Row"": ""1"",
1533
""@Grid.Column"": ""0"",
1534
""@Text"": ""Date Created"",
1535
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1536
""@Grid.Row"": ""2"",
1540
""@Grid.Column"": ""0"",
1541
""@Text"": ""Date Due"",
1542
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1543
""@Grid.Row"": ""3"",
1547
""@Grid.Column"": ""0"",
1548
""@Text"": ""Author"",
1549
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1550
""@Grid.Row"": ""4"",
1554
""@Grid.Column"": ""0"",
1555
""@Text"": ""Container"",
1556
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1557
""@Grid.Row"": ""5"",
1561
""@Grid.Column"": ""0"",
1562
""@Text"": ""Enclosed?"",
1563
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1564
""@Grid.Row"": ""6"",
1568
""@Grid.Column"": ""0"",
1569
""@Text"": ""Assignee"",
1570
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1571
""@Grid.Row"": ""7"",
1578
Assert.AreEqual(expectedJson, json);
1580
XNode node = JsonConvert.DeserializeXNode(json);
1582
string xaml2 = node.ToString();
1584
string expectedXaml = @"<Grid xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:toolkit=""clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"" Style=""{StaticResource trimFormGrid}"" x:Name=""TrimObjectForm"">
1585
<Grid.ColumnDefinitions>
1586
<ColumnDefinition Width=""63*"" />
1587
<ColumnDefinition Width=""320*"" />
1588
</Grid.ColumnDefinitions>
1589
<Grid.RowDefinitions xmlns="""">
1598
</Grid.RowDefinitions>
1599
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding TypedTitle, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordTypedTitle"" Grid.Column=""1"" Grid.Row=""0"" xmlns="""" />
1600
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding ExternalReference, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordExternalReference"" Grid.Column=""1"" Grid.Row=""1"" xmlns="""" />
1601
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Author, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordAuthor"" Grid.Column=""1"" Grid.Row=""4"" xmlns="""" />
1602
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Container, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordContainer"" Grid.Column=""1"" Grid.Row=""5"" xmlns="""" />
1603
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding IsEnclosed, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordIsEnclosed"" Grid.Column=""1"" Grid.Row=""6"" xmlns="""" />
1604
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Assignee, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordAssignee"" Grid.Column=""1"" Grid.Row=""7"" xmlns="""" />
1605
<toolkit:DatePicker Style=""{StaticResource trimFormGrid_DP}"" Value=""{Binding DateCreated, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordDateCreated"" Grid.Column=""1"" Grid.Row=""2"" />
1606
<toolkit:DatePicker Style=""{StaticResource trimFormGrid_DP}"" Value=""{Binding DateDue, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordDateDue"" Grid.Column=""1"" Grid.Row=""3"" />
1607
<TextBlock Grid.Column=""0"" Text=""Title (Free Text Part)"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""0"" xmlns="""" />
1608
<TextBlock Grid.Column=""0"" Text=""External ID"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""1"" xmlns="""" />
1609
<TextBlock Grid.Column=""0"" Text=""Date Created"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""2"" xmlns="""" />
1610
<TextBlock Grid.Column=""0"" Text=""Date Due"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""3"" xmlns="""" />
1611
<TextBlock Grid.Column=""0"" Text=""Author"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""4"" xmlns="""" />
1612
<TextBlock Grid.Column=""0"" Text=""Container"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""5"" xmlns="""" />
1613
<TextBlock Grid.Column=""0"" Text=""Enclosed?"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""6"" xmlns="""" />
1614
<TextBlock Grid.Column=""0"" Text=""Assignee"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""7"" xmlns="""" />
1617
Assert.AreEqual(expectedXaml, xaml2);
1622
public void DeserializeXmlNodeDefaultNamespace()
1624
string xaml = @"<Grid xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:toolkit=""clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"" Style=""{StaticResource trimFormGrid}"" x:Name=""TrimObjectForm"">
1625
<Grid.ColumnDefinitions>
1626
<ColumnDefinition Width=""63*"" />
1627
<ColumnDefinition Width=""320*"" />
1628
</Grid.ColumnDefinitions>
1629
<Grid.RowDefinitions xmlns="""">
1638
</Grid.RowDefinitions>
1639
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding TypedTitle, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordTypedTitle"" Grid.Column=""1"" Grid.Row=""0"" xmlns="""" />
1640
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding ExternalReference, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordExternalReference"" Grid.Column=""1"" Grid.Row=""1"" xmlns="""" />
1641
<toolkit:DatePicker Style=""{StaticResource trimFormGrid_DP}"" Value=""{Binding DateCreated, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordDateCreated"" Grid.Column=""1"" Grid.Row=""2"" />
1642
<toolkit:DatePicker Style=""{StaticResource trimFormGrid_DP}"" Value=""{Binding DateDue, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordDateDue"" Grid.Column=""1"" Grid.Row=""3"" />
1643
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Author, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordAuthor"" Grid.Column=""1"" Grid.Row=""4"" xmlns="""" />
1644
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Container, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordContainer"" Grid.Column=""1"" Grid.Row=""5"" xmlns="""" />
1645
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding IsEnclosed, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordIsEnclosed"" Grid.Column=""1"" Grid.Row=""6"" xmlns="""" />
1646
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Assignee, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordAssignee"" Grid.Column=""1"" Grid.Row=""7"" xmlns="""" />
1647
<TextBlock Grid.Column=""0"" Text=""Title (Free Text Part)"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""0"" xmlns="""" />
1648
<TextBlock Grid.Column=""0"" Text=""External ID"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""1"" xmlns="""" />
1649
<TextBlock Grid.Column=""0"" Text=""Date Created"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""2"" xmlns="""" />
1650
<TextBlock Grid.Column=""0"" Text=""Date Due"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""3"" xmlns="""" />
1651
<TextBlock Grid.Column=""0"" Text=""Author"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""4"" xmlns="""" />
1652
<TextBlock Grid.Column=""0"" Text=""Container"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""5"" xmlns="""" />
1653
<TextBlock Grid.Column=""0"" Text=""Enclosed?"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""6"" xmlns="""" />
1654
<TextBlock Grid.Column=""0"" Text=""Assignee"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""7"" xmlns="""" />
1657
XmlDocument document = new XmlDocument();
1658
document.LoadXml(xaml);
1660
string json = JsonConvert.SerializeXmlNode(document, Formatting.Indented);
1662
string expectedJson = @"{
1664
""@xmlns"": ""http://schemas.microsoft.com/winfx/2006/xaml/presentation"",
1665
""@xmlns:x"": ""http://schemas.microsoft.com/winfx/2006/xaml"",
1666
""@xmlns:toolkit"": ""clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"",
1667
""@Style"": ""{StaticResource trimFormGrid}"",
1668
""@x:Name"": ""TrimObjectForm"",
1669
""Grid.ColumnDefinitions"": {
1670
""ColumnDefinition"": [
1675
""@Width"": ""320*""
1679
""Grid.RowDefinitions"": {
1681
""RowDefinition"": [
1694
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1695
""@Text"": ""{Binding TypedTitle, Converter={StaticResource trimPropertyConverter}}"",
1696
""@Name"": ""RecordTypedTitle"",
1697
""@Grid.Column"": ""1"",
1698
""@Grid.Row"": ""0"",
1702
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1703
""@Text"": ""{Binding ExternalReference, Converter={StaticResource trimPropertyConverter}}"",
1704
""@Name"": ""RecordExternalReference"",
1705
""@Grid.Column"": ""1"",
1706
""@Grid.Row"": ""1"",
1710
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1711
""@Text"": ""{Binding Author, Converter={StaticResource trimPropertyConverter}}"",
1712
""@Name"": ""RecordAuthor"",
1713
""@Grid.Column"": ""1"",
1714
""@Grid.Row"": ""4"",
1718
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1719
""@Text"": ""{Binding Container, Converter={StaticResource trimPropertyConverter}}"",
1720
""@Name"": ""RecordContainer"",
1721
""@Grid.Column"": ""1"",
1722
""@Grid.Row"": ""5"",
1726
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1727
""@Text"": ""{Binding IsEnclosed, Converter={StaticResource trimPropertyConverter}}"",
1728
""@Name"": ""RecordIsEnclosed"",
1729
""@Grid.Column"": ""1"",
1730
""@Grid.Row"": ""6"",
1734
""@Style"": ""{StaticResource trimFormGrid_TB}"",
1735
""@Text"": ""{Binding Assignee, Converter={StaticResource trimPropertyConverter}}"",
1736
""@Name"": ""RecordAssignee"",
1737
""@Grid.Column"": ""1"",
1738
""@Grid.Row"": ""7"",
1742
""toolkit:DatePicker"": [
1744
""@Style"": ""{StaticResource trimFormGrid_DP}"",
1745
""@Value"": ""{Binding DateCreated, Converter={StaticResource trimPropertyConverter}}"",
1746
""@Name"": ""RecordDateCreated"",
1747
""@Grid.Column"": ""1"",
1748
""@Grid.Row"": ""2""
1751
""@Style"": ""{StaticResource trimFormGrid_DP}"",
1752
""@Value"": ""{Binding DateDue, Converter={StaticResource trimPropertyConverter}}"",
1753
""@Name"": ""RecordDateDue"",
1754
""@Grid.Column"": ""1"",
1755
""@Grid.Row"": ""3""
1760
""@Grid.Column"": ""0"",
1761
""@Text"": ""Title (Free Text Part)"",
1762
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1763
""@Grid.Row"": ""0"",
1767
""@Grid.Column"": ""0"",
1768
""@Text"": ""External ID"",
1769
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1770
""@Grid.Row"": ""1"",
1774
""@Grid.Column"": ""0"",
1775
""@Text"": ""Date Created"",
1776
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1777
""@Grid.Row"": ""2"",
1781
""@Grid.Column"": ""0"",
1782
""@Text"": ""Date Due"",
1783
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1784
""@Grid.Row"": ""3"",
1788
""@Grid.Column"": ""0"",
1789
""@Text"": ""Author"",
1790
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1791
""@Grid.Row"": ""4"",
1795
""@Grid.Column"": ""0"",
1796
""@Text"": ""Container"",
1797
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1798
""@Grid.Row"": ""5"",
1802
""@Grid.Column"": ""0"",
1803
""@Text"": ""Enclosed?"",
1804
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1805
""@Grid.Row"": ""6"",
1809
""@Grid.Column"": ""0"",
1810
""@Text"": ""Assignee"",
1811
""@Style"": ""{StaticResource trimFormGrid_LBL}"",
1812
""@Grid.Row"": ""7"",
1819
Assert.AreEqual(expectedJson, json);
1821
XmlNode node = JsonConvert.DeserializeXmlNode(json);
1823
StringWriter sw = new StringWriter();
1824
XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings
1827
OmitXmlDeclaration = true
1829
node.WriteTo(writer);
1832
string xaml2 = sw.ToString();
1834
string expectedXaml = @"<Grid xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:toolkit=""clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"" Style=""{StaticResource trimFormGrid}"" x:Name=""TrimObjectForm"">
1835
<Grid.ColumnDefinitions>
1836
<ColumnDefinition Width=""63*"" />
1837
<ColumnDefinition Width=""320*"" />
1838
</Grid.ColumnDefinitions>
1839
<Grid.RowDefinitions xmlns="""">
1848
</Grid.RowDefinitions>
1849
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding TypedTitle, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordTypedTitle"" Grid.Column=""1"" Grid.Row=""0"" xmlns="""" />
1850
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding ExternalReference, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordExternalReference"" Grid.Column=""1"" Grid.Row=""1"" xmlns="""" />
1851
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Author, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordAuthor"" Grid.Column=""1"" Grid.Row=""4"" xmlns="""" />
1852
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Container, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordContainer"" Grid.Column=""1"" Grid.Row=""5"" xmlns="""" />
1853
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding IsEnclosed, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordIsEnclosed"" Grid.Column=""1"" Grid.Row=""6"" xmlns="""" />
1854
<TextBox Style=""{StaticResource trimFormGrid_TB}"" Text=""{Binding Assignee, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordAssignee"" Grid.Column=""1"" Grid.Row=""7"" xmlns="""" />
1855
<toolkit:DatePicker Style=""{StaticResource trimFormGrid_DP}"" Value=""{Binding DateCreated, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordDateCreated"" Grid.Column=""1"" Grid.Row=""2"" />
1856
<toolkit:DatePicker Style=""{StaticResource trimFormGrid_DP}"" Value=""{Binding DateDue, Converter={StaticResource trimPropertyConverter}}"" Name=""RecordDateDue"" Grid.Column=""1"" Grid.Row=""3"" />
1857
<TextBlock Grid.Column=""0"" Text=""Title (Free Text Part)"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""0"" xmlns="""" />
1858
<TextBlock Grid.Column=""0"" Text=""External ID"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""1"" xmlns="""" />
1859
<TextBlock Grid.Column=""0"" Text=""Date Created"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""2"" xmlns="""" />
1860
<TextBlock Grid.Column=""0"" Text=""Date Due"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""3"" xmlns="""" />
1861
<TextBlock Grid.Column=""0"" Text=""Author"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""4"" xmlns="""" />
1862
<TextBlock Grid.Column=""0"" Text=""Container"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""5"" xmlns="""" />
1863
<TextBlock Grid.Column=""0"" Text=""Enclosed?"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""6"" xmlns="""" />
1864
<TextBlock Grid.Column=""0"" Text=""Assignee"" Style=""{StaticResource trimFormGrid_LBL}"" Grid.Row=""7"" xmlns="""" />
1867
Assert.AreEqual(expectedXaml, xaml2);
1871
public void DeserializeAttributePropertyNotAtStart()
1873
string json = @"{""item"": {""@action"": ""update"", ""@itemid"": ""1"", ""elements"": [{""@action"": ""none"", ""@id"": ""2""},{""@action"": ""none"", ""@id"": ""3""}],""@description"": ""temp""}}";
1875
XmlDocument xmldoc = JsonConvert.DeserializeXmlNode(json);
1877
Assert.AreEqual(@"<item action=""update"" itemid=""1"" description=""temp""><elements action=""none"" id=""2"" /><elements action=""none"" id=""3"" /></item>", xmldoc.InnerXml);
b'\\ No newline at end of file'