diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
index e8b5c229..d9b92926 100644
--- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
+++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
@@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
-#if !SILVERLIGHT
+#if !SILVERLIGHT || WINDOWS_PHONE
using System;
using System.Collections.Generic;
using System.Xml;
@@ -35,7 +35,8 @@ using System.Linq;
namespace Newtonsoft.Json.Converters
{
- #region Wrappers
+ #region XmlNodeWrappers
+#if !SILVERLIGHT
internal class XmlDocumentWrapper : XmlNodeWrapper, IXmlDocument
{
private XmlDocument _document;
@@ -262,7 +263,10 @@ namespace Newtonsoft.Json.Converters
get { return _node.NamespaceURI; }
}
}
+#endif
+ #endregion
+ #region Interfaces
internal interface IXmlDocument : IXmlNode
{
IXmlNode CreateComment(string text);
@@ -304,8 +308,10 @@ namespace Newtonsoft.Json.Converters
string NamespaceURI { get; }
object WrappedNode { get; }
}
+ #endregion
- #if !NET20
+ #region XNodeWrappers
+#if !NET20
internal class XDeclarationWrapper : XObjectWrapper, IXmlDeclaration
{
internal readonly XDeclaration _declaration;
@@ -725,7 +731,7 @@ namespace Newtonsoft.Json.Converters
#endregion
///
- /// Converts an to and from JSON.
+ /// Converts XML to and from JSON.
///
public class XmlNodeConverter : JsonConverter
{
@@ -752,16 +758,7 @@ namespace Newtonsoft.Json.Converters
/// The value.
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
- IXmlNode node;
-
- if (value is XmlNode)
- node = new XmlNodeWrapper((XmlNode)value);
-#if !NET20
- else if (value is XObject)
- node = XContainerWrapper.WrapNode((XObject)value);
-#endif
- else
- throw new ArgumentException("Value must be an XmlNode", "value");
+ IXmlNode node = WrapXml(value);
XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
PushParentNamespaces(node, manager);
@@ -771,6 +768,20 @@ namespace Newtonsoft.Json.Converters
writer.WriteEndObject();
}
+ private IXmlNode WrapXml(object value)
+ {
+#if !NET20
+ if (value is XObject)
+ return XContainerWrapper.WrapNode((XObject)value);
+#endif
+#if !SILVERLIGHT
+ if (value is XmlNode)
+ return new XmlNodeWrapper((XmlNode)value);
+#endif
+
+ throw new ArgumentException("Value must be an XML object.", "value");
+ }
+
private void PushParentNamespaces(IXmlNode node, XmlNamespaceManager manager)
{
List parentElements = null;
@@ -1022,20 +1033,11 @@ namespace Newtonsoft.Json.Converters
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
- IXmlDocument document;
- IXmlNode rootNode;
+ IXmlDocument document = null;
+ IXmlNode rootNode = null;
- if (typeof (XmlNode).IsAssignableFrom(objectType))
- {
- if (objectType != typeof (XmlDocument))
- throw new JsonSerializationException("XmlNodeConverter only supports deserializing XmlDocuments");
-
- XmlDocument d = new XmlDocument();
- document = new XmlDocumentWrapper(d);
- rootNode = document;
- }
#if !NET20
- else if (typeof(XObject).IsAssignableFrom(objectType))
+ if (typeof(XObject).IsAssignableFrom(objectType))
{
if (objectType != typeof (XDocument) && objectType != typeof (XElement))
throw new JsonSerializationException("XmlNodeConverter only supports deserializing XDocument or XElement.");
@@ -1045,10 +1047,20 @@ namespace Newtonsoft.Json.Converters
rootNode = document;
}
#endif
- else
+#if !SILVERLIGHT
+ if (typeof(XmlNode).IsAssignableFrom(objectType))
{
- throw new JsonSerializationException("Unexpected type when converting XML: " + objectType);
+ if (objectType != typeof (XmlDocument))
+ throw new JsonSerializationException("XmlNodeConverter only supports deserializing XmlDocuments");
+
+ XmlDocument d = new XmlDocument();
+ document = new XmlDocumentWrapper(d);
+ rootNode = document;
}
+#endif
+
+ if (document == null || rootNode == null)
+ throw new JsonSerializationException("Unexpected type when converting XML: " + objectType);
if (reader.TokenType != JsonToken.StartObject)
throw new JsonSerializationException("XmlNodeConverter can only convert JSON that begins with an object.");
@@ -1414,12 +1426,14 @@ namespace Newtonsoft.Json.Converters
///
public override bool CanConvert(Type valueType)
{
- if (typeof(XmlNode).IsAssignableFrom(valueType))
- return true;
#if !NET20
if (typeof(XObject).IsAssignableFrom(valueType))
return true;
#endif
+#if !SILVERLIGHT
+ if (typeof(XmlNode).IsAssignableFrom(valueType))
+ return true;
+#endif
return false;
}
diff --git a/Src/Newtonsoft.Json/JsonConvert.cs b/Src/Newtonsoft.Json/JsonConvert.cs
index 54b9458b..38b56cda 100644
--- a/Src/Newtonsoft.Json/JsonConvert.cs
+++ b/Src/Newtonsoft.Json/JsonConvert.cs
@@ -30,7 +30,7 @@ using Newtonsoft.Json.Utilities;
using System.Xml;
using Newtonsoft.Json.Converters;
using System.Text;
-#if !NET20 && !SILVERLIGHT
+#if !NET20 && (!SILVERLIGHT || WINDOWS_PHONE)
using System.Xml.Linq;
#endif
@@ -761,55 +761,6 @@ namespace Newtonsoft.Json
}
}
-#if !NET20 && !SILVERLIGHT
- ///
- /// Serializes the to a JSON string.
- ///
- /// The node to convert to JSON.
- /// A JSON string of the XNode.
- public static string SerializeXNode(XObject node)
- {
- return SerializeXNode(node, Formatting.None);
- }
-
- ///
- /// Serializes the to a JSON string.
- ///
- /// The node to convert to JSON.
- /// Indicates how the output is formatted.
- /// A JSON string of the XNode.
- public static string SerializeXNode(XObject node, Formatting formatting)
- {
- XmlNodeConverter converter = new XmlNodeConverter();
-
- return SerializeObject(node, formatting, converter);
- }
-
- ///
- /// Deserializes the from a JSON string.
- ///
- /// The JSON string.
- /// The deserialized XNode
- public static XDocument DeserializeXNode(string value)
- {
- return DeserializeXNode(value, null);
- }
-
- ///
- /// Deserializes the from a JSON string nested in a root elment.
- ///
- /// The JSON string.
- /// The name of the root element to append when deserializing.
- /// The deserialized XNode
- public static XDocument DeserializeXNode(string value, string deserializeRootElementName)
- {
- XmlNodeConverter converter = new XmlNodeConverter();
- converter.DeserializeRootElementName = deserializeRootElementName;
-
- return (XDocument)DeserializeObject(value, typeof(XDocument), converter);
- }
-#endif
-
#if !SILVERLIGHT
///
/// Serializes the XML node to a JSON string.
@@ -858,5 +809,54 @@ namespace Newtonsoft.Json
return (XmlDocument)DeserializeObject(value, typeof(XmlDocument), converter);
}
#endif
+
+#if !NET20 && (!SILVERLIGHT || WINDOWS_PHONE)
+ ///
+ /// Serializes the to a JSON string.
+ ///
+ /// The node to convert to JSON.
+ /// A JSON string of the XNode.
+ public static string SerializeXNode(XObject node)
+ {
+ return SerializeXNode(node, Formatting.None);
+ }
+
+ ///
+ /// Serializes the to a JSON string.
+ ///
+ /// The node to convert to JSON.
+ /// Indicates how the output is formatted.
+ /// A JSON string of the XNode.
+ public static string SerializeXNode(XObject node, Formatting formatting)
+ {
+ XmlNodeConverter converter = new XmlNodeConverter();
+
+ return SerializeObject(node, formatting, converter);
+ }
+
+ ///
+ /// Deserializes the from a JSON string.
+ ///
+ /// The JSON string.
+ /// The deserialized XNode
+ public static XDocument DeserializeXNode(string value)
+ {
+ return DeserializeXNode(value, null);
+ }
+
+ ///
+ /// Deserializes the from a JSON string nested in a root elment.
+ ///
+ /// The JSON string.
+ /// The name of the root element to append when deserializing.
+ /// The deserialized XNode
+ public static XDocument DeserializeXNode(string value, string deserializeRootElementName)
+ {
+ XmlNodeConverter converter = new XmlNodeConverter();
+ converter.DeserializeRootElementName = deserializeRootElementName;
+
+ return (XDocument)DeserializeObject(value, typeof(XDocument), converter);
+ }
+#endif
}
}
\ No newline at end of file
diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj
index a686d4ca..6604d0fe 100644
--- a/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj
+++ b/Src/Newtonsoft.Json/Newtonsoft.Json.WindowsPhone.csproj
@@ -50,6 +50,7 @@
+