Beispiel #1
0
        public static StringValueDictionary <T> ReadDictionary <T> (this XmlReader reader, ITypeResolver resolver)
        {
            var result = new StringValueDictionary <T>();

            ReadDictionary(reader, result, resolver);

            return(result);
        }
Beispiel #2
0
        public static void ReadDictionary <T> (XmlReader reader, StringValueDictionary <T> result, ITypeResolver resolver)
        {
            if (reader.NodeType != XmlNodeType.Element)
            {
                throw new InvalidDataException("Provided XmlReader must be at the start of an element");
            }

            string key;
            int    typeId;
            T      value;
            var    typeIds     = new Dictionary <int, Type>();
            var    serializers = new Dictionary <int, XmlSerializer>();
            string sentinel    = reader.Name;
            var    outputType  = typeof(T);

            reader.ReadToDescendant("types");

            if (!reader.IsEmptyElement &&
                (reader.GetAttribute("nil") == null) &&
                (reader.GetAttribute("xsi:nil") == null))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "types")
                    {
                        break;
                    }
                    else if (reader.NodeType == XmlNodeType.Element && reader.Name == "type")
                    {
                        int    id       = int.Parse(reader.GetAttribute("id"), CultureInfo.InvariantCulture);
                        string typeName = reader.GetAttribute("name");
                        var    t        = resolver.NameToType(typeName);
                        if (outputType.IsAssignableFrom(t))
                        {
                            typeIds[id] = t;
                        }
                        else
                        {
                            throw new InvalidDataException(String.Format("Cannot store an {0} into a Dictionary<String, {1}>.", t.Name, outputType.Name));
                        }
                    }
                }
            }

            reader.ReadToFollowing("values");
            if (!reader.IsEmptyElement)
            {
                reader.Read();
                while (!reader.EOF)
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        key    = reader.GetAttribute("key");
                        typeId = int.Parse(reader.GetAttribute("typeId"), CultureInfo.InvariantCulture);

                        XmlSerializer ser;
                        if (!serializers.TryGetValue(typeId, out ser))
                        {
                            ser = new XmlSerializer(typeIds[typeId]);
                            serializers[typeId] = ser;
                        }
                        value = (T)ser.Deserialize(reader);

                        if (key == null)
                        {
                            if (value is INamedObject)
                            {
                                key = ((INamedObject)value).Name;
                            }
                        }

                        if (key == null)
                        {
                            throw new InvalidDataException(String.Format("Item has no key and is not INamedObject: {0}", value));
                        }

                        result.Add(key, value);
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "values")
                    {
                        break;
                    }
                    else
                    {
                        if (!reader.Read())
                        {
                            break;
                        }
                    }
                }
            }

            while (!reader.EOF && (reader.NodeType != XmlNodeType.EndElement || reader.Name != sentinel))
            {
                reader.Read();
            }
        }