public XmlArray DoToXmlArray_FromDictionary(Type type, Type keyType, Type valueType, IDictionary dictionary, string mainNodeName, XmlMappingContainer mappings = null)
        {
            var result = new XmlArray(mainNodeName);

            var valueTypeName = valueType.Name;

            foreach (var keyValue in dictionary)
            {
                var entry = (DictionaryEntry)keyValue;

                // Int32 for example
                var keyValueXmlObject = new XmlObject(valueTypeName);

                // key
                var keyString = this.ResolveDictionaryKey(keyType, entry.Key);
                keyValueXmlObject.Add("Key", new XmlString("Key", keyString));

                // value
                var resultValue = this.ToXmlValue(valueType, entry.Value, "Value", mappings);
                keyValueXmlObject.Add("Value", resultValue);

                result.Add(keyValueXmlObject);
            }

            return(result);
        }
        public object ToDictionary(Type type, XmlArray xmlArray, XmlMappingContainer mappings = null)
        {
            var keyType   = this.assemblyInfoService.GetDictionaryKeyType(type);
            var valueType = this.assemblyInfoService.GetDictionaryValueType(type);

            var result = this.assemblyInfoService.CreateInstance(type) as IDictionary;

            if (result == null)
            {
                throw new JsonLibException("Cannot create dictionary");
            }

            foreach (var xmlValue in xmlArray.Values)
            {
                var xmlObject = xmlValue as XmlObject;
                if (xmlObject == null || xmlObject.Values.Count != 2)
                {
                    throw new JsonLibException("Cannot resolve dictionary from xml");
                }

                var xmlEntryKey = xmlObject.Values.ElementAt(0).Value;
                var key         = this.ResolveDictionaryKey(keyType, xmlEntryKey);

                var xmlEntryValue = xmlObject.Values.ElementAt(1).Value;
                var value         = this.Resolve(valueType, xmlEntryValue, mappings);

                result.Add(key, value);
            }

            return(result);
        }
        public XmlArray ConvertToArray(XmlObject xmlObject)
        {
            var result = new XmlArray(xmlObject.NodeName);

            foreach (var xmlValue in xmlObject.Values)
            {
                result.Add(xmlValue.Value);
            }
            return(result);
        }
        public object ToList(Type type, XmlArray xmlArray, XmlMappingContainer mappings = null)
        {
            var singleItemType = type.GetGenericArguments()[0];
            var listType       = typeof(List <>).MakeGenericType(singleItemType);
            var result         = this.assemblyInfoService.CreateInstance(listType) as IList;

            foreach (var xmlValue in xmlArray.Values)
            {
                var value = this.Resolve(singleItemType, xmlValue, mappings);
                result.Add(value);
            }
            return(result);
        }
        public XmlArray ToXmlArray(XElement element)
        {
            var nodeName = element.Name.LocalName;
            var result   = new XmlArray(nodeName);

            foreach (var child in element.Elements())
            {
                var xmlValue = this.FindNextElement(child);
                result.Add(xmlValue);
            }

            return(result);
        }
        public object ToArray(Type type, XmlArray xmlArray, XmlMappingContainer mappings = null)
        {
            var singleItemType = type.GetTypeInfo().GetElementType();
            var result         = Array.CreateInstance(singleItemType, xmlArray.Values.Count);
            int index          = 0;

            foreach (var xmlValue in xmlArray.Values)
            {
                var value = this.Resolve(singleItemType, xmlValue, mappings);
                result.SetValue(value, index);
                index++;
            }
            return(result);
        }
        public object ToEnumerable(Type type, XmlArray xmlArray, XmlMappingContainer mappings = null)
        {
            if (this.assemblyInfoService.IsArray(type))
            {
                return(this.ToArray(type, xmlArray, mappings));
            }
            else if (this.assemblyInfoService.IsDictionary(type))
            {
                return(this.ToDictionary(type, xmlArray, mappings));
            }
            else if (this.assemblyInfoService.IsGenericType(type))
            {
                return(this.ToList(type, xmlArray, mappings));
            }

            throw new JsonLibException("Type is not enumerable " + type.Name);
        }
        public string ToArray(XmlArray xmlArray)
        {
            if (xmlArray.IsNil)
            {
                return(this.xmlService.GetNilNode(xmlArray.NodeName));
            }
            else
            {
                var result = new List <string>();

                foreach (var xmlValue in xmlArray.Values)
                {
                    var valueResult = this.ToValue(xmlValue);
                    result.Add(valueResult);
                }

                return(this.xmlService.GetNode(xmlArray.NodeName, string.Join("", result)));
            }
        }
        protected XmlArray DoToXmlArray(Type type, Type singleItemType, string nodeName, IEnumerable enumerable, XmlMappingContainer mappings = null)
        {
            // string[],List<string>, int[]...MyEnum[],Guid[], DateTime[]
            // int?[],... DateTime?[], Guid?[], MyEnum?[]
            // MyItem[]

            var result = new XmlArray(nodeName);

            if (enumerable != null)
            {
                foreach (var value in enumerable)
                {
                    var resultValue = this.ToXmlValue(singleItemType, value, mappings);
                    result.Add(resultValue);
                }
            }
            else
            {
                result.SetNil();
            }
            return(result);
        }
Ejemplo n.º 10
0
 public XmlObject AddArray(string nodeName, XmlArray value)
 {
     return(this.Add(nodeName, value));
 }