Esempio n. 1
0
 protected void HandleId(string id, XmlFormatterDeserializer deserializer, object instance)
 {
     if (id != null)
     {
         deserializer.References.Add(id, instance);
     }
 }
Esempio n. 2
0
        public override object DeserializeEmptyContent(XmlReader reader, XmlFormatterDeserializer deserializer, string id)
        {
            var instance = CreateInstance();

            HandleId(id, deserializer, instance);
            if (OnDeserializing != null)
            {
                OnDeserializing.Invoke(instance, new object [] { new StreamingContext(StreamingContextStates.All) });
            }
            try {
                if (RuntimeType.IsArray)
                {
                    return(((ArrayList)instance).ToArray(element_type));
                }
                else
                {
                    return(instance);
                }
            } finally {
                if (OnDeserialized != null)
                {
                    OnDeserialized.Invoke(instance, new object [] { new StreamingContext(StreamingContextStates.All) });
                }
            }
        }
Esempio n. 3
0
        public override object DeserializeEmptyContent(XmlReader reader, XmlFormatterDeserializer deserializer, string id)
        {
            object instance = CreateInstance();

            HandleId(id, deserializer, instance);
            return(instance);
        }
Esempio n. 4
0
        public override object DeserializeEmptyContent(XmlReader reader, XmlFormatterDeserializer deserializer, string id)
        {
            if (!flag_attr)
            {
                throw new SerializationException(String.Format("Enum value '' is invalid for type '{0}' and cannot be deserialized.", RuntimeType));
            }
            var instance = Enum.ToObject(RuntimeType, 0);

            HandleId(id, deserializer, instance);
            return(instance);
        }
Esempio n. 5
0
        public override object DeserializeObject(XmlReader reader, XmlFormatterDeserializer deserializer)
        {
#if NET_2_1
            IXmlSerializable ixs = (IXmlSerializable)Activator.CreateInstance(RuntimeType);
#else
            IXmlSerializable ixs = (IXmlSerializable)Activator.CreateInstance(RuntimeType, true);
#endif

            HandleId(reader, deserializer, ixs);

            ixs.ReadXml(reader);
            return(ixs);
        }
        public override object DeserializeContent(XmlReader reader, XmlFormatterDeserializer deserializer, string id)
        {
            object instance = CreateInstance();

            HandleId(id, deserializer, instance);
            if (OnDeserializing != null)
            {
                OnDeserializing.Invoke(instance, new object [] { new StreamingContext(StreamingContextStates.All) });
            }
            int depth = reader.NodeType == XmlNodeType.None ? reader.Depth : reader.Depth - 1;

            while (reader.NodeType == XmlNodeType.Element && reader.Depth > depth)
            {
                object elem = deserializer.Deserialize(element_type, reader);
                if (instance is IList)
                {
                    ((IList)instance).Add(elem);
                }
                else if (add_method != null)
                {
                    add_method.Invoke(instance, new object [] { elem });
                }
                else
                {
                    throw new NotImplementedException(String.Format("Type {0} is not supported", RuntimeType));
                }
                reader.MoveToContent();
            }
            try
            {
                if (RuntimeType.IsArray)
                {
                    return(((ArrayList)instance).ToArray(element_type));
                }
                return(instance);
            }
            finally
            {
                if (OnDeserialized != null)
                {
                    OnDeserialized.Invoke(instance, new object [] { new StreamingContext(StreamingContextStates.All) });
                }
            }
        }
Esempio n. 7
0
        public override object DeserializeContent(XmlReader reader, XmlFormatterDeserializer deserializer, string id)
        {
            string value = reader.NodeType != XmlNodeType.Text ? String.Empty : reader.ReadContentAsString();

            HandleId(id, deserializer, value);

            if (value != String.Empty)
            {
                if (flag_attr && value.IndexOf(' ') != -1)
                {
                    long flags = 0L;
                    foreach (string flag in value.Split(' '))
                    {
                        foreach (EnumMemberInfo emi in enum_members)
                        {
                            if (emi.XmlName == flag)
                            {
                                flags |= Convert.ToInt64(emi.Value);
                                break;
                            }
                        }
                    }
                    return(Enum.ToObject(RuntimeType, flags));
                }
                else
                {
                    foreach (EnumMemberInfo emi in enum_members)
                    {
                        if (emi.XmlName == value)
                        {
                            return(emi.Value);
                        }
                    }
                }
            }

            if (!flag_attr)
            {
                throw new SerializationException(String.Format("Enum value '{0}' is invalid for type '{1}' and cannot be deserialized.", value, RuntimeType));
            }
            return(Enum.ToObject(RuntimeType, 0));
        }
        public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
        {
            int startTypeCount = known_types.Count;

            known_types.Add(type);

            bool isEmpty = reader.IsEmptyElement;

            object ret = XmlFormatterDeserializer.Deserialize(reader, type,
                                                              known_types, surrogate, DataContractResolver, default_resolver, root_name.Value, root_ns.Value, verifyObjectName);

            // remove temporarily-added known types for
            // rootType and object graph type.
            while (known_types.Count > startTypeCount)
            {
                known_types.RemoveAt(startTypeCount);
            }

            return(ret);
        }
Esempio n. 9
0
        public override object DeserializeContent(XmlReader reader, XmlFormatterDeserializer deserializer, string id)
        {
            object instance = CreateInstance();

            HandleId(id, deserializer, instance);
            int depth = reader.NodeType == XmlNodeType.None ? reader.Depth : reader.Depth - 1;

            while (reader.NodeType == XmlNodeType.Element && reader.Depth > depth)
            {
                if (reader.IsEmptyElement)
                {
                    throw new XmlException(String.Format("Unexpected empty element for dictionary entry: name {0}", reader.Name));
                }
                // FIXME: sloppy parsing
                reader.ReadStartElement();                 // item_qname.Name, item_qname.Namespace);
                reader.MoveToContent();
                object key = deserializer.Deserialize(key_type, reader);
                reader.MoveToContent();
                object val = deserializer.Deserialize(value_type, reader);
                reader.MoveToContent();
                reader.ReadEndElement();                  // of pair
                reader.MoveToContent();

                if (instance is IDictionary)
                {
                    ((IDictionary)instance).Add(key, val);
                }
                else if (add_method != null)
                {
                    add_method.Invoke(instance, new object [] { key, val });
                }
                else
                {
                    throw new NotImplementedException(String.Format("Type {0} is not supported", RuntimeType));
                }
            }
            return(instance);
        }
Esempio n. 10
0
        public virtual object DeserializeObject(XmlReader reader, XmlFormatterDeserializer deserializer)
        {
            bool   isEmpty = reader.IsEmptyElement;
            string id      = reader.GetAttribute("Id", KnownTypeCollection.MSSimpleNamespace);

            reader.ReadStartElement();
            reader.MoveToContent();

            object res;

            if (isEmpty)
            {
                res = DeserializeEmptyContent(reader, deserializer, id);
            }
            else
            {
                res = DeserializeContent(reader, deserializer, id);
            }

            reader.MoveToContent();
            if (!isEmpty && reader.NodeType == XmlNodeType.EndElement)
            {
                reader.ReadEndElement();
            }
            else if (!isEmpty && !reader.EOF && reader.NodeType != XmlNodeType.EndElement)
            {
                var li = reader as IXmlLineInfo;
                throw new SerializationException(String.Format("Deserializing type '{3}'. Expecting state 'EndElement'. Encountered state '{0}' with name '{1}' with namespace '{2}'.{4}",
                                                               reader.NodeType,
                                                               reader.Name,
                                                               reader.NamespaceURI,
                                                               RuntimeType.FullName,
                                                               li != null && li.HasLineInfo() ? String.Format(" {0}({1},{2})", reader.BaseURI, li.LineNumber, li.LinePosition) : String.Empty));
            }
            return(res);
        }
Esempio n. 11
0
        object DeserializeContent(XmlReader reader, XmlFormatterDeserializer deserializer, string id, bool empty)
        {
            object instance = FormatterServices.GetUninitializedObject(RuntimeType);

            HandleId(id, deserializer, instance);

            if (OnDeserializing != null)
            {
                OnDeserializing.Invoke(instance, new object [] { new StreamingContext(StreamingContextStates.All) });
            }

            int depth = reader.NodeType == XmlNodeType.None ? reader.Depth : reader.Depth - 1;

            bool [] filled = new bool [Members.Count];
            bool [] nsmatched = new bool [Members.Count];
            int     memberInd = -1, ordered = -1;

            while (!empty && reader.NodeType == XmlNodeType.Element && reader.Depth > depth)
            {
                DataMemberInfo dmi          = null;
                int            i            = 0;
                bool           nsmatchedOne = false;
                for (; i < Members.Count; i++)                   // unordered
                {
                    if (Members [i].Order >= 0)
                    {
                        break;
                    }
                    if (reader.LocalName == Members [i].XmlName)
                    {
                        memberInd    = i;
                        dmi          = Members [i];
                        nsmatchedOne = (dmi.XmlRootNamespace == null || reader.NamespaceURI == dmi.XmlRootNamespace);
                        if (nsmatchedOne)
                        {
                            break;
                        }
                    }
                }
                for (i = Math.Max(i, ordered); i < Members.Count; i++)                    // ordered
                {
                    if (dmi != null)
                    {
                        break;
                    }
                    if (reader.LocalName == Members [i].XmlName)
                    {
                        ordered      = i;
                        memberInd    = i;
                        dmi          = Members [i];
                        nsmatchedOne = (dmi.XmlRootNamespace == null || reader.NamespaceURI == dmi.XmlRootNamespace);
                        if (nsmatchedOne)
                        {
                            break;
                        }
                    }
                }

                if (dmi == null)
                {
                    reader.Skip();
                    reader.MoveToContent();
                    continue;
                }
                if (filled [memberInd] && nsmatched [memberInd])
                {
                    // The strictly-corresponding member (i.e. that matches namespace URI too, not only local name) already exists, so skip this element.
                    reader.Skip();
                    reader.MoveToContent();
                    continue;
                }
                nsmatched [memberInd] = nsmatchedOne;
                SetValue(dmi, instance, deserializer.Deserialize(dmi.MemberType, reader));
                filled [memberInd] = true;
                reader.MoveToContent();
            }
            for (int i = 0; i < Members.Count; i++)
            {
                if (!filled [i] && Members [i].IsRequired)
                {
                    throw MissingRequiredMember(Members [i], reader);
                }
            }

            if (OnDeserialized != null)
            {
                OnDeserialized.Invoke(instance, new object [] { new StreamingContext(StreamingContextStates.All) });
            }

            return(instance);
        }
Esempio n. 12
0
 public virtual object DeserializeContent(XmlReader reader,
                                          XmlFormatterDeserializer deserializer, string id)
 {
     return(DeserializeContent(reader, deserializer, id, false));
 }
Esempio n. 13
0
 protected void HandleId(XmlReader reader, XmlFormatterDeserializer deserializer, object instance)
 {
     HandleId(reader.GetAttribute("Id", KnownTypeCollection.MSSimpleNamespace), deserializer, instance);
 }
Esempio n. 14
0
 public override object DeserializeEmptyContent(XmlReader reader, XmlFormatterDeserializer deserializer, string id)
 {
     return(DeserializeContent(reader, deserializer, id));
 }