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 !(NET35 || NET20 || PORTABLE)
28
using System.Collections.Generic;
29
using System.ComponentModel;
33
using System.Runtime.Serialization;
36
using Newtonsoft.Json.Converters;
37
using Newtonsoft.Json.Linq;
39
using NUnit.Framework;
41
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
42
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
43
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
45
using Newtonsoft.Json.Serialization;
46
using Newtonsoft.Json.Tests.TestObjects;
47
using Newtonsoft.Json.Utilities;
48
using System.Globalization;
49
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
51
namespace Newtonsoft.Json.Tests.Documentation
53
public class ConvertingJsonAndXmlTests
55
public void SerializeXmlNode()
57
#region SerializeXmlNode
58
string xml = @"<?xml version='1.0' standalone='no'?>
62
<url>http://www.google.com</url>
66
<url>http://www.yahoo.com</url>
70
XmlDocument doc = new XmlDocument();
73
string jsonText = JsonConvert.SerializeXmlNode(doc);
77
// "@standalone": "no"
84
// "url": "http://www.google.com"
89
// "url": "http://www.yahoo.com"
97
public void DeserializeXmlNode()
99
#region DeserializeXmlNode
110
'url': 'http://www.google.com'
115
'url': 'http://www.yahoo.com'
121
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
122
// <?xml version="1.0" standalone="no"?>
126
// <url>http://www.google.com</url>
129
// <name>Louis</name>
130
// <url>http://www.yahoo.com</url>
136
public void ForceJsonArray()
138
#region ForceJsonArray
139
string xml = @"<person id='1'>
141
<url>http://www.google.com</url>
145
XmlDocument doc = new XmlDocument();
148
string json = JsonConvert.SerializeXmlNode(doc);
153
// "url": "http://www.google.com",
158
xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1'>
160
<url>http://www.google.com</url>
161
<role json:Array='true'>Admin</role>
164
doc = new XmlDocument();
167
json = JsonConvert.SerializeXmlNode(doc);
172
// "url": "http://www.google.com",
b'\\ No newline at end of file'