コード例 #1
0
        /// <summary>
        /// Removing this from the IXmlSerializable Implementation because engineers keep exposing the SerialzableDictionary object
        /// and that cannot be allowed because nobody else can deserialize.  It is for persistance only so the logic was moved here.
        /// </summary>
        /// <param name="doc">The doc.</param>
        /// <returns></returns>
        private IList<string> convertFromXml(XmlDocument doc)
        {
            XmlReader reader = new XmlNodeReader(doc);

            //First check empty element and return if found
            bool wasEmpty = reader.IsEmptyElement;
            reader.Read();
            if (wasEmpty)
                return null;

            IList<string> result = new List<string>();
            reader.ReadStartElement(DocumentElement);

            if (reader.NodeType != XmlNodeType.Element)
                return result;

            //Loop through the nodes representing this object in the reader
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                //First try the optimized format
                //string key = reader.GetAttribute(KEY_ATTRIBUTE);
                string typeName = reader.GetAttribute(TYPE_ATTRIBUTE);

                //Read the Xml element representing a new key & value pair
                reader.ReadStartElement(ITEM_ELEMENT);

                //if (String.IsNullOrEmpty(key))
                //{
                //    //Key is not being stoed as an attribute - this must be xml in the old format
                //    //KEEP THIS CODE FOR BACKWARDS COMPATIBILITY

                //    //First we need to get the type of the object written to the Xml during serialization so that
                //    //we can create a new serializer that understands how to deserialize this type.
                //    typeName = reader.GetAttribute(TYPE_ATTRIBUTE);

                //    //Read the Xml element representing the key in this key & value pair
                //    reader.ReadStartElement(KEY_ELEMENT);

                //    //Allright now create the serializer and deserialize the defined object type
                //    XmlSerializer keySerializer = new XmlSerializer(typeof(string));
                //    //key = (string)keySerializer.Deserialize(reader);

                //    if (key == null)
                //        throw new ApplicationException(String.Format("Null key encountered on line {0}",
                //                                                       reader.Depth));

                //    //Read the end of the key element
                //    reader.ReadEndElement();
                //}

                Type valuetype = (typeName != null ? Type.GetType(typeName) : typeof(object)) ?? typeof(object);

                //Read the Xml element representing the value in this key & value pair
                reader.ReadStartElement(VALUE_ELEMENT);

                //Now create the serialize and deserialize the object type defined from the type attribute
                XmlSerializer valueSerializer = new XmlSerializer(valuetype);

                //HACK!!!
                //Make sure you catch any errors caused by invalid types cannot be deserialized. For example this whole process
                //kept blowing up because the type ould not be serialized.
                string value;// = (TValue) valueSerializer.Deserialize(reader);
                try { value = (string)valueSerializer.Deserialize(reader); }
                catch (Exception)
                {
                    value = default(string);
                    //skip top the end of the current element
                    reader.Skip();
                }

                //Read the end of the value element
                reader.ReadEndElement();

                //Now add the deserialized objects to the hashtable.
                result.Add(value);

                //Read the end of the element holding the key and value elements
                if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == ITEM_ELEMENT)
                    reader.ReadEndElement();
                reader.MoveToContent();
            }

            //All done - read the ending element
            reader.ReadEndElement();

            return result;
        }