コード例 #1
0
        /// <summary>
        /// To the JToken.
        /// </summary>
        /// <param name="xml">The XML.</param>
        /// <returns>JToken.</returns>
        internal static JToken InternalToJToken(this XElement xml)
        {
            JToken result = null;
            JTokenType tokenType;

            if (Enum.TryParse<JTokenType>(xml.GetAttributeValue(attributeType), out tokenType))
            {
                if (xml != null)
                {
                    switch (tokenType)
                    {
                        case JTokenType.Array:
                            var array = new JArray();

                            foreach (var one in xml.Elements())
                            {
                                array.AddIfNotNull(InternalToJToken(one));
                            }

                            result = array;
                            break;
                        //Simple types
                        case JTokenType.Date:
                            result = JToken.FromObject(Convert.ToDateTime(xml.Value));
                            break;
                        case JTokenType.Integer:
                        case JTokenType.Float:
                            result = JToken.Parse(xml.Value);
                            break;
                        case JTokenType.Boolean:
                            result = JToken.FromObject(xml.Value.ToBoolean(false));
                            break;
                        //Complex types
                        case JTokenType.Object:
                            var jObject = new JObject();
                            foreach (var one in xml.Elements())
                            {
                                // Keep compatibility for those which not use nodeNamePrefix.
                                var name = one.Name.LocalName.StartsWith(nodeNamePrefix) ? one.Name.LocalName.Substring(nodeNamePrefix.Length) : one.Name.LocalName;
                                jObject.Add(name, InternalToJToken(one));
                            }
                            result = jObject;
                            break;
                        case JTokenType.Property:
                        case JTokenType.Raw:
                            break;
                        case JTokenType.TimeSpan:
                            result = JToken.FromObject(new TimeSpan(xml.Value.ToInt64()));
                            break;
                        case JTokenType.String:
                            result = JToken.FromObject(xml.Value);
                            break;
                        case JTokenType.Uri:
                            result = JToken.FromObject(new Uri(xml.Value));
                            break;
                        case JTokenType.Bytes:
                            result = JToken.FromObject(Encoding.UTF8.GetBytes(xml.Value));
                            break;
                        case JTokenType.Null:
                            result = NullJToken;
                            break;
                        case JTokenType.Undefined:
                            result = UndefinedJToken;
                            break;
                        //Following option should be ignored.
                        case JTokenType.Comment:
                        case JTokenType.Constructor:
                        case JTokenType.None:
                        default:
                            break;
                    }
                }

            }
            return result;
        }
コード例 #2
0
        /// <summary>
        /// Internals the dexmlize.
        /// </summary>
        /// <param name="xml">The XML.</param>
        /// <returns></returns>
        private static JToken InternalDexmlize(this XElement xml)
        {
            JToken result = null;
            JTokenType tokenType;

            if (Enum.TryParse<JTokenType>(xml.GetAttributeValue(attributeType), out tokenType))
            {
                if (xml != null)
                {
                    switch (tokenType)
                    {
                        case JTokenType.Array:
                            var array = new JArray();

                            foreach (var one in xml.Elements())
                            {
                                array.AddIfNotNull(InternalDexmlize(one));
                            }

                            result = array;
                            break;
                        //Simple types
                        case JTokenType.Date:
                            result = JToken.FromObject(Convert.ToDateTime(xml.Value));
                            break;
                        case JTokenType.Integer:
                        case JTokenType.Float:
                            result = JToken.Parse(xml.Value);
                            break;
                        case JTokenType.Boolean:
                            result = JToken.FromObject(xml.Value.ToBoolean(false));
                            break;
                        //Complex types
                        case JTokenType.Object:
                            var jObject = new JObject();
                            foreach (var one in xml.Elements())
                            {
                                if (one.Name.LocalName.Equals(nodeProperty, StringComparison.OrdinalIgnoreCase))
                                {
                                    var name = one.GetAttributeValue(attributeName);

                                    if (!string.IsNullOrWhiteSpace(name))
                                    {
                                        jObject.Add(name, InternalDexmlize(one));
                                    }
                                }
                            }
                            result = jObject;
                            break;
                        case JTokenType.Property:
                        case JTokenType.Raw:
                            break;
                        case JTokenType.TimeSpan:
                            result = JToken.FromObject(new TimeSpan(xml.Value.ToInt64()));
                            break;
                        case JTokenType.String:
                            result = JToken.FromObject(xml.Value);
                            break;
                        case JTokenType.Uri:
                            result = JToken.FromObject(new Uri(xml.Value));
                            break;
                        case JTokenType.Bytes:
                            result = JToken.FromObject(Encoding.UTF8.GetBytes(xml.Value));
                            break;
                        case JTokenType.Null:
                            result = NullJToken;
                            break;
                        case JTokenType.Undefined:
                            result = UndefinedJToken;
                            break;
                        //Following option should be ignored.
                        case JTokenType.Comment:
                        case JTokenType.Constructor:
                        case JTokenType.None:
                        default:
                            break;
                    }
                }

            }
            return result;
        }