Ejemplo n.º 1
0
        public void ReadXml(System.Xml.XmlReader reader)
        {
            XmlSerializer keySerializer   = new XmlSerializer(typeof(TKey));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
            bool          wasEmpty        = reader.IsEmptyElement;

            reader.Read();
            if (wasEmpty)
            {
                return;
            }
            while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
            {
                reader.ReadStartElement("I");                //Item
                reader.ReadStartElement("K");                //Key
                TKey key = (TKey)keySerializer.Deserialize(reader);
                if (key != null)
                {
                    key = (TKey)XmlConverter.XmlUnescapeRecursion(typeof(TKey), key);
                }
                reader.ReadEndElement();
                reader.ReadStartElement("V");                //Value
                TValue value = (TValue)valueSerializer.Deserialize(reader);
                if (value != null)
                {
                    value = (TValue)XmlConverter.XmlUnescapeRecursion(typeof(TValue), value);
                }
                this.Add(key, value);
                reader.ReadEndElement();
                reader.ReadEndElement();
                reader.MoveToContent();
            }
            reader.ReadEndElement();
        }
Ejemplo n.º 2
0
        public static DataTransferObject Deserialize(string data)
        {
            StringReader strReader = new StringReader(data);
            //XmlReader reader=XmlReader.Create(strReader);
            XmlTextReader reader      = new XmlTextReader(strReader);
            string        strNodeName = "";

            while (reader.Read())
            {
                if (reader.NodeType != XmlNodeType.Element)
                {
                    continue;
                }
                strNodeName = reader.Name;
                break;
            }
            //strReader.Close();
            //reader.Close();
            Type type = Type.GetType("OpenDentBusiness." + strNodeName);
            //StringReader strReader2=new StringReader(data);
            //XmlReader reader2=XmlReader.Create(strReader2);
            XmlSerializer      serializer = new XmlSerializer(type);
            DataTransferObject retVal     = (DataTransferObject)serializer.Deserialize(reader);

            strReader.Close();
            reader.Close();
            if (retVal.Params != null)
            {
                foreach (DtoObject dtoCur in retVal.Params.Where(x => x != null && x.Obj != null))
                {
                    dtoCur.Obj = XmlConverter.XmlUnescapeRecursion(dtoCur.Obj.GetType(), dtoCur.Obj);
                }
            }
            return(retVal);
        }
Ejemplo n.º 3
0
        ///<summary>Should accept any type.  Tested types include System types, OD types, Arrays, Lists, arrays of DtoObject, null DataObjectBase, null arrays, null Lists.  But not DataTable or DataSet.  If we find a type that isn't supported, then we need to add it.  Types that are currently unsupported include Arrays of DataObjectBase that contain a null.  Lists that contain nulls are untested and may be an issue for DataObjectBase.</summary>
        public static T Deserialize <T>(string xmlData)
        {
            Type type = typeof(T);

            /*later.  I don't think arrays will null objects will be an issue.
             * if(type.IsArray) {
             *      Type arrayType=type.GetElementType();
             *      if(arrayType.BaseType==typeof(DataObjectBase)) {
             *              //split into items
             *      }
             * }*/
            if (type.IsGenericType)             //List<>
            //because the built-in deserializer does not handle null list<>, but instead returns an empty list.
            //<ArrayOfDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:nil="true" />
            {
                if (Regex.IsMatch(xmlData, "<ArrayOf[^>]*xsi:nil=\"true\""))
                {
                    return(default(T));                   //null
                }
            }
            StringReader strReader = new StringReader(xmlData);
            //XmlReader reader=XmlReader.Create(strReader);
            XmlTextReader reader = new XmlTextReader(strReader);
            XmlSerializer serializer;
            T             retVal;

            if (type == typeof(Color))
            {
                serializer = new XmlSerializer(typeof(int));
                retVal     = (T)((object)Color.FromArgb((int)serializer.Deserialize(reader)));
            }
            else if (type == typeof(TimeSpan))
            {
                serializer = new XmlSerializer(typeof(long));
                retVal     = (T)((object)TimeSpan.FromTicks((long)serializer.Deserialize(reader)));
            }
            else if (type.IsInterface)
            {
                //For methods that return an interface, we serialize the return object as a DtoObject.
                serializer = new XmlSerializer(typeof(DtoObject));
                retVal     = (T)((DtoObject)serializer.Deserialize(reader)).Obj;
            }
            else
            {
                serializer = new XmlSerializer(type);
                retVal     = (T)serializer.Deserialize(reader);
                if (retVal != null)
                {
                    retVal = (T)XmlConverter.XmlUnescapeRecursion(type, retVal);
                }
            }
            strReader.Close();
            reader.Close();
            return(retVal);
        }