Ejemplo n.º 1
0
        /// <summary>
        /// Loads mappings from the specified file name.
        /// </summary>
        /// <param name="fileName">Name of the mappings file.</param>
        /// <returns></returns>
        static public Mappings Load(String fileName)
        {
            if (fileName == null)
                return new Mappings();

            // Deserialize the ADX mappings from an XML file
            Mappings mappings = new Mappings();
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Mappings));

                FileStream adxStream = new FileStream(fileName, FileMode.Open);
                XmlReader xmlReader = XmlReader.Create(adxStream);

                // Ignore mal-formed XML for now
                XmlDeserializationEvents events = new XmlDeserializationEvents();
                events.OnUnknownElement = Mappings.OnLoadUnknownElements;
                events.OnUnknownAttribute = Mappings.OnLoadUnknownAttributes;
                events.OnUnknownNode = Mappings.OnLoadUnknownNode;
                events.OnUnreferencedObject = Mappings.OnLoadUnreferencedObjects;

                // Get the ADX mappings
                mappings = xmlSerializer.Deserialize(xmlReader, events) as Mappings;

                adxStream.Close();
            }

            // Trap and throw the any exceptions higher up
            catch (System.Exception exc)
            {
                throw exc;
            }

            return mappings;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deserializes the <sites /> section from the applicationhosts.config file into an ObservableCollection&lt;site&gt;
        /// </summary>
        private void Load()
        {
            if (File.Exists(_configurationService.ApplicationHostConfigPath))
            {
                var doc = XDocument.Load(_configurationService.ApplicationHostConfigPath);

                var events = new XmlDeserializationEvents();
                events.OnUnknownAttribute = (s, e) => { Debug.WriteLine("Unknown Attributed"); };
                events.OnUnknownElement = (s, e) => { Debug.WriteLine("Unknwon Element: " + e.Element.Name); };
                events.OnUnknownNode = (s, e) => { Debug.WriteLine("Unknown Node: " + e.Name); };
                events.OnUnreferencedObject = (s, e) => { Debug.WriteLine("Unreferenced Object"); };

                var deserializer = new XmlSerializer(typeof(List<site>), new XmlRootAttribute("sites"));

                var sitesElement = doc.Descendants("sites").FirstOrDefault();

                if (sitesElement != null)
                {
                    var reader = sitesElement.CreateReader();
                    var s = (List<site>)deserializer.Deserialize(reader, events);

                    //s = s.Select(site =>
                    //{
                    //    var node = doc.Descendants().Where(d => (d.Name == "site") && (d.Attribute("id").Value == site.id)).FirstOrDefault();
                    //    if (node != null)
                    //    {

                    //        var comment = node.Elements().DescendantNodesAndSelf().Where(n => n.NodeType == XmlNodeType.Comment).Select(c => c as XComment).FirstOrDefault();

                    //        if (comment != null)
                    //        {
                    //            site.subdomain = comment.Value;
                    //        }
                    //        else
                    //        {
                    //            site.subdomain = site.name;
                    //        }
                    //    }
                    //    return site;
                    //}).ToList();

                    var svms = s.Select(site => new SiteViewModel(site)).ToList();
                    this.Sites = new ObservableCollection<SiteViewModel>(svms);
                }
            }
        }
Ejemplo n.º 3
0
		public object Deserialize (XmlReader xmlReader, XmlDeserializationEvents events)
		{
			throw new NotImplementedException ();
		}
 public XmlSerializer(XmlTypeMapping xmlTypeMapping)
 {
     this.events       = new XmlDeserializationEvents();
     this.tempAssembly = GenerateTempAssembly(xmlTypeMapping);
     this.mapping      = xmlTypeMapping;
 }
Ejemplo n.º 5
0
 /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.Deserialize3"]/*' />
 public object Deserialize(XmlReader xmlReader, XmlDeserializationEvents events)
 {
     return Deserialize(xmlReader, null, events);
 }
 public string ReadHeaders(XmlReader reader, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, string envelopeNS, string encodingStyle, bool checkRequiredHeaders)
 {
     string headerElementName = null;
     reader.MoveToContent();
     if (!reader.IsStartElement("Header", envelopeNS))
     {
         if ((checkRequiredHeaders && (mappings != null)) && (mappings.Length > 0))
         {
             headerElementName = GetHeaderElementName(mappings[0].headerType);
         }
         return headerElementName;
     }
     if (reader.IsEmptyElement)
     {
         reader.Skip();
         return headerElementName;
     }
     this.unknownHeaders = new SoapHeaderCollection();
     this.unreferencedHeaders = new SoapHeaderCollection();
     this.currentThread = Thread.CurrentThread.GetHashCode();
     this.envelopeNS = envelopeNS;
     int depth = reader.Depth;
     reader.ReadStartElement();
     reader.MoveToContent();
     XmlDeserializationEvents events = new XmlDeserializationEvents {
         OnUnknownElement = new XmlElementEventHandler(this.OnUnknownElement),
         OnUnreferencedObject = new UnreferencedObjectEventHandler(this.OnUnreferencedObject)
     };
     TraceMethod caller = Tracing.On ? new TraceMethod(this, "ReadHeaders", new object[0]) : null;
     if (Tracing.On)
     {
         Tracing.Enter(Tracing.TraceId("TraceReadHeaders"), caller, new TraceMethod(serializer, "Deserialize", new object[] { reader, encodingStyle }));
     }
     object[] objArray = (object[]) serializer.Deserialize(reader, encodingStyle, events);
     if (Tracing.On)
     {
         Tracing.Exit(Tracing.TraceId("TraceReadHeaders"), caller);
     }
     for (int i = 0; i < objArray.Length; i++)
     {
         if (objArray[i] != null)
         {
             SoapHeader header = (SoapHeader) objArray[i];
             header.DidUnderstand = true;
             headers.Add(header);
         }
         else if (checkRequiredHeaders && (headerElementName == null))
         {
             headerElementName = GetHeaderElementName(mappings[i].headerType);
         }
     }
     this.currentThread = 0;
     this.envelopeNS = null;
     foreach (SoapHeader header2 in this.unreferencedHeaders)
     {
         headers.Add(header2);
     }
     this.unreferencedHeaders = null;
     foreach (SoapHeader header3 in this.unknownHeaders)
     {
         headers.Add(header3);
     }
     this.unknownHeaders = null;
     while ((depth < reader.Depth) && reader.Read())
     {
     }
     if (reader.NodeType == XmlNodeType.EndElement)
     {
         reader.Read();
     }
     return headerElementName;
 }
 public object Deserialize(XmlReader xmlReader, XmlDeserializationEvents events)
 {
     return(Deserialize(xmlReader, null, events));
 }
        private object DeserializePrimitive(XmlReader xmlReader, XmlDeserializationEvents events)
        {
            XmlSerializationPrimitiveReader reader = new XmlSerializationPrimitiveReader();

            reader.Init(xmlReader, events, null, null);
            object o;

            switch (_primitiveType.GetTypeCode())
            {
            case TypeCode.String:
                o = reader.Read_string();
                break;

            case TypeCode.Int32:
                o = reader.Read_int();
                break;

            case TypeCode.Boolean:
                o = reader.Read_boolean();
                break;

            case TypeCode.Int16:
                o = reader.Read_short();
                break;

            case TypeCode.Int64:
                o = reader.Read_long();
                break;

            case TypeCode.Single:
                o = reader.Read_float();
                break;

            case TypeCode.Double:
                o = reader.Read_double();
                break;

            case TypeCode.Decimal:
                o = reader.Read_decimal();
                break;

            case TypeCode.DateTime:
                o = reader.Read_dateTime();
                break;

            case TypeCode.Char:
                o = reader.Read_char();
                break;

            case TypeCode.Byte:
                o = reader.Read_unsignedByte();
                break;

            case TypeCode.SByte:
                o = reader.Read_byte();
                break;

            case TypeCode.UInt16:
                o = reader.Read_unsignedShort();
                break;

            case TypeCode.UInt32:
                o = reader.Read_unsignedInt();
                break;

            case TypeCode.UInt64:
                o = reader.Read_unsignedLong();
                break;

            default:
                if (_primitiveType == typeof(XmlQualifiedName))
                {
                    o = reader.Read_QName();
                }
                else if (_primitiveType == typeof(byte[]))
                {
                    o = reader.Read_base64Binary();
                }
                else if (_primitiveType == typeof(Guid))
                {
                    o = reader.Read_guid();
                }
                else if (_primitiveType == typeof(TimeSpan))
                {
                    o = reader.Read_TimeSpan();
                }
                else
                {
                    throw new InvalidOperationException(SR.Format(SR.XmlUnxpectedType, _primitiveType.FullName));
                }
                break;
            }
            return(o);
        }
 public XmlSerializer(Type type, string defaultNamespace)
 {
     this.events = new XmlDeserializationEvents();
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     this.mapping = GetKnownMapping(type, defaultNamespace);
     if (this.mapping != null)
     {
         this.primitiveType = type;
     }
     else
     {
         this.tempAssembly = cache[defaultNamespace, type];
         if (this.tempAssembly == null)
         {
             lock (cache)
             {
                 this.tempAssembly = cache[defaultNamespace, type];
                 if (this.tempAssembly == null)
                 {
                     XmlSerializerImplementation implementation;
                     Assembly assembly = TempAssembly.LoadGeneratedAssembly(type, defaultNamespace, out implementation);
                     if (assembly == null)
                     {
                         this.mapping = new XmlReflectionImporter(defaultNamespace).ImportTypeMapping(type, null, defaultNamespace);
                         this.tempAssembly = GenerateTempAssembly(this.mapping, type, defaultNamespace);
                     }
                     else
                     {
                         this.mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
                         this.tempAssembly = new TempAssembly(new XmlMapping[] { this.mapping }, assembly, implementation);
                     }
                 }
                 cache.Add(defaultNamespace, type, this.tempAssembly);
             }
         }
         if (this.mapping == null)
         {
             this.mapping = XmlReflectionImporter.GetTopLevelMapping(type, defaultNamespace);
         }
     }
 }
Ejemplo n.º 10
0
 public ReflectionXmlSerializationReader(XmlMapping mapping, XmlReader xmlReader, XmlDeserializationEvents events, string encodingStyle)
 {
     Init(xmlReader, events, encodingStyle, tempAssembly: null);
     _mapping = mapping;
 }
 protected XmlSerializer()
 {
     this.events = new XmlDeserializationEvents();
 }
 protected XmlSerializer()
 {
     this.events = new XmlDeserializationEvents();
 }
        private object DeserializePrimitive(XmlReader xmlReader, XmlDeserializationEvents events)
        {
            XmlSerializationPrimitiveReader reader = new XmlSerializationPrimitiveReader();

            reader.Init(xmlReader, events, null, null);
            switch (Type.GetTypeCode(this.primitiveType))
            {
            case TypeCode.Boolean:
                return(reader.Read_boolean());

            case TypeCode.Char:
                return(reader.Read_char());

            case TypeCode.SByte:
                return(reader.Read_byte());

            case TypeCode.Byte:
                return(reader.Read_unsignedByte());

            case TypeCode.Int16:
                return(reader.Read_short());

            case TypeCode.UInt16:
                return(reader.Read_unsignedShort());

            case TypeCode.Int32:
                return(reader.Read_int());

            case TypeCode.UInt32:
                return(reader.Read_unsignedInt());

            case TypeCode.Int64:
                return(reader.Read_long());

            case TypeCode.UInt64:
                return(reader.Read_unsignedLong());

            case TypeCode.Single:
                return(reader.Read_float());

            case TypeCode.Double:
                return(reader.Read_double());

            case TypeCode.Decimal:
                return(reader.Read_decimal());

            case TypeCode.DateTime:
                return(reader.Read_dateTime());

            case TypeCode.String:
                return(reader.Read_string());
            }
            if (this.primitiveType == typeof(XmlQualifiedName))
            {
                return(reader.Read_QName());
            }
            if (this.primitiveType == typeof(byte[]))
            {
                return(reader.Read_base64Binary());
            }
            if (this.primitiveType != typeof(Guid))
            {
                throw new InvalidOperationException(Res.GetString("XmlUnxpectedType", new object[] { this.primitiveType.FullName }));
            }
            return(reader.Read_guid());
        }
 /// <summary>
 ///     Deserializes the object using the data contained by the specified <see cref="T:System.Xml.XmlReader"/>.
 /// </summary>
 /// <returns>
 ///     The object being deserialized.
 /// </returns>
 /// <param name="xmlReader">An instance of the <see cref="T:System.Xml.XmlReader"/> class used to read the document.</param><param name="encodingStyle">The encoding used.</param><param name="events">An instance of the <see cref="T:System.Xml.Serialization.XmlDeserializationEvents"/> class. </param>
 public object Deserialize(XmlReader xmlReader,
                           string encodingStyle,
                           XmlDeserializationEvents events)
 {
     return this.XmlSerializerInstance.Deserialize(xmlReader, encodingStyle, events);
 }
 /// <summary>
 ///     Deserializes an XML document contained by the specified <see cref="T:System.Xml.XmlReader"/> and allows the overriding of events that occur during deserialization.
 /// </summary>
 /// <returns>
 ///     The <see cref="T:System.Object"/> being deserialized.
 /// </returns>
 /// <param name="xmlReader">The <see cref="T:System.Xml.XmlReader"/> that contains the document to deserialize.</param><param name="events">An instance of the <see cref="T:System.Xml.Serialization.XmlDeserializationEvents"/> class. </param>
 public object Deserialize(XmlReader xmlReader,
                           XmlDeserializationEvents events)
 {
     return this.XmlSerializerInstance.Deserialize(xmlReader, null, events);
 }
 internal void Init(XmlReader r, XmlDeserializationEvents events, string encodingStyle, TempAssembly tempAssembly)
 {
     this.events = events;
     if (checkDeserializeAdvances)
     {
         this.countingReader = new XmlCountingReader(r);
         this.r = this.countingReader;
     }
     else
     {
         this.r = r;
     }
     this.d = null;
     this.soap12 = encodingStyle == "http://www.w3.org/2003/05/soap-encoding";
     base.Init(tempAssembly);
     this.schemaNsID = r.NameTable.Add("http://www.w3.org/2001/XMLSchema");
     this.schemaNs2000ID = r.NameTable.Add("http://www.w3.org/2000/10/XMLSchema");
     this.schemaNs1999ID = r.NameTable.Add("http://www.w3.org/1999/XMLSchema");
     this.schemaNonXsdTypesNsID = r.NameTable.Add("http://microsoft.com/wsdl/types/");
     this.instanceNsID = r.NameTable.Add("http://www.w3.org/2001/XMLSchema-instance");
     this.instanceNs2000ID = r.NameTable.Add("http://www.w3.org/2000/10/XMLSchema-instance");
     this.instanceNs1999ID = r.NameTable.Add("http://www.w3.org/1999/XMLSchema-instance");
     this.soapNsID = r.NameTable.Add("http://schemas.xmlsoap.org/soap/encoding/");
     this.soap12NsID = r.NameTable.Add("http://www.w3.org/2003/05/soap-encoding");
     this.schemaID = r.NameTable.Add("schema");
     this.wsdlNsID = r.NameTable.Add("http://schemas.xmlsoap.org/wsdl/");
     this.wsdlArrayTypeID = r.NameTable.Add("arrayType");
     this.nullID = r.NameTable.Add("null");
     this.nilID = r.NameTable.Add("nil");
     this.typeID = r.NameTable.Add("type");
     this.arrayTypeID = r.NameTable.Add("arrayType");
     this.itemTypeID = r.NameTable.Add("itemType");
     this.arraySizeID = r.NameTable.Add("arraySize");
     this.arrayID = r.NameTable.Add("Array");
     this.urTypeID = r.NameTable.Add("anyType");
     this.InitIDs();
 }
Ejemplo n.º 17
0
 public object InvokeReader(int methodIndex, XmlReader xmlReader, XmlDeserializationEvents events) {
     if (!allAssembliesAllowPartialTrust)
         new PermissionSet(PermissionState.Unrestricted).Demand();
     XmlSerializationReader reader = (XmlSerializationReader)Activator.CreateInstance(readerType);
     reader.Init(xmlReader, events);
     ResolveEventHandler resolver = new ResolveEventHandler(this.OnAssemblyResolve);
     assemblyResolveThread = Thread.CurrentThread.GetHashCode();
     AppDomain.CurrentDomain.AssemblyResolve += resolver;
     object ret = methods[methodIndex].readMethod.Invoke(reader, emptyObjectArray);
     AppDomain.CurrentDomain.AssemblyResolve -= resolver;
     assemblyResolveThread = 0;
     return ret;
 }
 public XmlSerializer(XmlTypeMapping xmlTypeMapping)
 {
     this.events = new XmlDeserializationEvents();
     this.tempAssembly = GenerateTempAssembly(xmlTypeMapping);
     this.mapping = xmlTypeMapping;
 }
Ejemplo n.º 19
0
 internal static XmlDeserializationEvents GetDeserializationEvents() {
     XmlDeserializationEvents events = new XmlDeserializationEvents();
     events.OnUnknownElement = new XmlElementEventHandler(OnUnknownElement);
     events.OnUnknownAttribute = new XmlAttributeEventHandler(OnUnknownAttribute);
     return events;
 }
Ejemplo n.º 20
0
 public object Deserialize(XmlReader xmlReader, XmlDeserializationEvents events)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 21
0
 internal void Init(XmlReader r, XmlDeserializationEvents events) {
     this.events = events;
     this.r = r;
     this.d = null;
     schemaNsID = r.NameTable.Add(XmlSchema.Namespace);
     schemaNs2000ID = r.NameTable.Add("http://www.w3.org/2000/10/XMLSchema");
     schemaNs1999ID = r.NameTable.Add("http://www.w3.org/1999/XMLSchema");
     schemaNonXsdTypesNsID = r.NameTable.Add(UrtTypes.Namespace);
     instanceNsID = r.NameTable.Add(XmlSchema.InstanceNamespace);
     instanceNs2000ID = r.NameTable.Add("http://www.w3.org/2000/10/XMLSchema-instance");
     instanceNs1999ID = r.NameTable.Add("http://www.w3.org/1999/XMLSchema-instance");
     soapNsID = r.NameTable.Add(Soap.Encoding);
     schemaID = r.NameTable.Add("schema");
     wsdlNsID = r.NameTable.Add(Wsdl.Namespace);
     wsdlArrayTypeID = r.NameTable.Add(Wsdl.ArrayType);
     nullID = r.NameTable.Add("null");
     nilID = r.NameTable.Add("nil");
     typeID = r.NameTable.Add("type");
     arrayTypeID = r.NameTable.Add("arrayType");
     urTypeID = r.NameTable.Add(Soap.UrType);
     InitIDs();
 }
Ejemplo n.º 22
0
        /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.Deserialize5"]/*' />
        public object Deserialize(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events) {
            events.sender = this;
            try {
                if (primitiveType != null) {
                    if (encodingStyle != null && encodingStyle.Length > 0) {
                        throw new InvalidOperationException(Res.GetString(Res.XmlInvalidEncodingNotEncoded1, encodingStyle));
                    }
                    return DeserializePrimitive(xmlReader, events);
                }
                else if (tempAssembly == null || typedSerializer) {
                    XmlSerializationReader reader = CreateReader();
                    reader.Init(xmlReader, events, encodingStyle, tempAssembly);
                    try {
                        return Deserialize(reader);
                    }
                    finally {
                        reader.Dispose();
                    }
                }
                else {
                    return tempAssembly.InvokeReader(mapping, xmlReader, events, encodingStyle);
                }
            }
            catch (Exception e) {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                    throw;
                }
                if (e is TargetInvocationException)
                    e = e.InnerException;

                if (xmlReader is IXmlLineInfo) {
                    IXmlLineInfo lineInfo = (IXmlLineInfo)xmlReader;
                    throw new InvalidOperationException(Res.GetString(Res.XmlSerializeErrorDetails, lineInfo.LineNumber.ToString(CultureInfo.InvariantCulture), lineInfo.LinePosition.ToString(CultureInfo.InvariantCulture)), e);
                }
                else {
                    throw new InvalidOperationException(Res.GetString(Res.XmlSerializeError), e);
                }
            }
            catch {
                if (xmlReader is IXmlLineInfo) {
                    IXmlLineInfo lineInfo = (IXmlLineInfo)xmlReader;
                    throw new InvalidOperationException(Res.GetString(Res.XmlSerializeErrorDetails, lineInfo.LineNumber.ToString(CultureInfo.InvariantCulture), lineInfo.LinePosition.ToString(CultureInfo.InvariantCulture)), null);
                }
                else {
                    throw new InvalidOperationException(Res.GetString(Res.XmlSerializeError), (Exception)null);
                }
            }
        }
Ejemplo n.º 23
0
        // return first missing header name;
        public string ReadHeaders(XmlReader reader, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, string envelopeNS, string encodingStyle, bool checkRequiredHeaders) {
            string missingHeader = null;
            reader.MoveToContent();
            if (!reader.IsStartElement(Soap.Element.Header, envelopeNS)) {
                if (checkRequiredHeaders && mappings != null && mappings.Length > 0)
                    missingHeader = GetHeaderElementName(mappings[0].headerType);
                return missingHeader;
            }
            if (reader.IsEmptyElement) { reader.Skip(); return missingHeader; }

            this.unknownHeaders = new SoapHeaderCollection();
            this.unreferencedHeaders = new SoapHeaderCollection();
            // thread hash code is used to differentiate between deserializations in event callbacks
            this.currentThread = Thread.CurrentThread.GetHashCode();
            this.envelopeNS = envelopeNS;

            int depth = reader.Depth;
            reader.ReadStartElement();
            reader.MoveToContent();

            XmlDeserializationEvents events = new XmlDeserializationEvents();
            events.OnUnknownElement = new XmlElementEventHandler(this.OnUnknownElement);
            events.OnUnreferencedObject = new UnreferencedObjectEventHandler(this.OnUnreferencedObject);

            TraceMethod caller = Tracing.On ? new TraceMethod(this, "ReadHeaders") : null;
            if (Tracing.On) Tracing.Enter(Tracing.TraceId(Res.TraceReadHeaders), caller, new TraceMethod(serializer, "Deserialize", reader, encodingStyle));
            object[] headerValues = (object[])serializer.Deserialize(reader, encodingStyle, events);
            if (Tracing.On) Tracing.Exit(Tracing.TraceId(Res.TraceReadHeaders), caller);
            for (int i = 0; i < headerValues.Length; i++) {
                if (headerValues[i] != null) {
                    SoapHeader header = (SoapHeader)headerValues[i];
                    header.DidUnderstand = true;
                    headers.Add(header);
                }
                else if (checkRequiredHeaders) {
                    // run time check for R2738 A MESSAGE MUST include all soapbind:headers specified on a wsdl:input or wsdl:output of a wsdl:operationwsdl:binding that describes it. 
                    if (missingHeader == null)
                        missingHeader = GetHeaderElementName(mappings[i].headerType);
                }
            }
            this.currentThread = 0;
            this.envelopeNS = null;

            foreach (SoapHeader header in this.unreferencedHeaders) {
                headers.Add(header);
            }
            this.unreferencedHeaders = null;

            foreach (SoapHeader header in this.unknownHeaders) {
                headers.Add(header);
            }
            this.unknownHeaders = null;

            // Consume soap:Body and soap:Envelope closing tags
            while (depth < reader.Depth && reader.Read()) {
                // Nothing, just read on
            }
            // consume end tag
            if (reader.NodeType == XmlNodeType.EndElement) {
                reader.Read();
            }

            return missingHeader;
        }
        private object DeserializeUsingReflection(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)
        {
            XmlMapping mapping = GetMapping();
            var        reader  = new ReflectionXmlSerializationReader(mapping, xmlReader, events, encodingStyle);

            return(reader.ReadObject());
        }
Ejemplo n.º 25
0
        protected XmlCatalogAndMetadata LoadXmlCatalog(string uri)
        {
            XmlSerializer ser = new XmlSerializer(typeof(XmlCatalog));

            XmlDeserializationEvents devents = new XmlDeserializationEvents();
            devents.OnUnknownAttribute = delegate(object sender, XmlAttributeEventArgs e)
            {
                logger.Warn("Unknown attribute {0}", e.Attr.Name);
            };

            devents.OnUnknownElement = delegate(object sender, XmlElementEventArgs e)
            {
                logger.Warn("Unknown element {0}", e.Element.Name);
            };

            XmlCatalog cat;

            using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(uri))
                cat = (XmlCatalog)ser.Deserialize(reader, devents);

            cat.LoadedFrom = uri;

            XmlCatalogAndMetadata xmd = new XmlCatalogAndMetadata(cat);
            xmd.Metadata.Add("uri", uri);

            return xmd;
        }
Ejemplo n.º 26
0
        private object DeserializePrimitive(XmlReader xmlReader, XmlDeserializationEvents events)
        {
            XmlSerializationPrimitiveReader reader = new XmlSerializationPrimitiveReader();
            reader.Init(xmlReader, events, null, null);
            object o;
            switch (_primitiveType.GetTypeCode())
            {
                case TypeCode.String:
                    o = reader.Read_string();
                    break;
                case TypeCode.Int32:
                    o = reader.Read_int();
                    break;
                case TypeCode.Boolean:
                    o = reader.Read_boolean();
                    break;
                case TypeCode.Int16:
                    o = reader.Read_short();
                    break;
                case TypeCode.Int64:
                    o = reader.Read_long();
                    break;
                case TypeCode.Single:
                    o = reader.Read_float();
                    break;
                case TypeCode.Double:
                    o = reader.Read_double();
                    break;
                case TypeCode.Decimal:
                    o = reader.Read_decimal();
                    break;
                case TypeCode.DateTime:
                    o = reader.Read_dateTime();
                    break;
                case TypeCode.Char:
                    o = reader.Read_char();
                    break;
                case TypeCode.Byte:
                    o = reader.Read_unsignedByte();
                    break;
                case TypeCode.SByte:
                    o = reader.Read_byte();
                    break;
                case TypeCode.UInt16:
                    o = reader.Read_unsignedShort();
                    break;
                case TypeCode.UInt32:
                    o = reader.Read_unsignedInt();
                    break;
                case TypeCode.UInt64:
                    o = reader.Read_unsignedLong();
                    break;

                default:
                    if (_primitiveType == typeof(XmlQualifiedName))
                    {
                        o = reader.Read_QName();
                    }
                    else if (_primitiveType == typeof(byte[]))
                    {
                        o = reader.Read_base64Binary();
                    }
                    else if (_primitiveType == typeof(Guid))
                    {
                        o = reader.Read_guid();
                    }
                    else if (_primitiveType == typeof(TimeSpan))
                    {
                        o = reader.Read_TimeSpan();
                    }
                    else
                    {
                        throw new InvalidOperationException(SR.Format(SR.XmlUnxpectedType, _primitiveType.FullName));
                    }
                    break;
            }
            return o;
        }
Ejemplo n.º 27
0
 public static object Deserialize(this XmlSerializer serializer, string xml, string encodingStyle, XmlDeserializationEvents events, bool ignoreNamespaces = false)
 {
     using (var stream = new StringReader(xml))
         using (var reader = CreateReader(stream, ignoreNamespaces))
         {
             return(serializer.Deserialize(reader, encodingStyle, events));
         }
 }
Ejemplo n.º 28
0
        /// <include file='doc\XmlSerializer.uex' path='docs/doc[@for="XmlSerializer.Deserialize5"]/*' />
        public object Deserialize(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)
        {
            events.sender = this;
            try
            {
                if (_primitiveType != null)
                {
                    if (encodingStyle != null && encodingStyle.Length > 0)
                    {
                        throw new InvalidOperationException(SR.Format(SR.XmlInvalidEncodingNotEncoded1, encodingStyle));
                    }
                    return DeserializePrimitive(xmlReader, events);
                }
#if !NET_NATIVE
                else if (_tempAssembly == null || _typedSerializer)
                {
                    XmlSerializationReader reader = CreateReader();
                    reader.Init(xmlReader, events, encodingStyle, _tempAssembly);
                    try
                    {
                        return Deserialize(reader);
                    }
                    finally
                    {
                        reader.Dispose();
                    }
                }
                else
                {
                    return _tempAssembly.InvokeReader(_mapping, xmlReader, events, encodingStyle);
                }
#else
                else
                {
                    if (this.innerSerializer == null)
                    {
                        throw new InvalidOperationException(SR.Format(SR.Xml_MissingSerializationCodeException, this.rootType, typeof(XmlSerializer).Name));
                    }

                    if (!string.IsNullOrEmpty(this.DefaultNamespace))
                    {
                        this.innerSerializer.DefaultNamespace = this.DefaultNamespace; 
                    }

                    XmlSerializationReader reader = this.innerSerializer.CreateReader();
                    reader.Init(xmlReader, encodingStyle);
                    try
                    {
                        return this.innerSerializer.Deserialize(reader);
                    }
                    finally
                    {
                        reader.Dispose();
                    }
                }
#endif
            }
            catch (Exception e)
            {
                if (e is TargetInvocationException)
                    e = e.InnerException;

                if (xmlReader is IXmlLineInfo)
                {
                    IXmlLineInfo lineInfo = (IXmlLineInfo)xmlReader;
                    throw new InvalidOperationException(SR.Format(SR.XmlSerializeErrorDetails, lineInfo.LineNumber.ToString(CultureInfo.InvariantCulture), lineInfo.LinePosition.ToString(CultureInfo.InvariantCulture)), e);
                }
                else
                {
                    throw new InvalidOperationException(SR.XmlSerializeError, e);
                }
            }
        }
 public XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace, string location, Evidence evidence)
 {
     this.events = new XmlDeserializationEvents();
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     XmlReflectionImporter importer = new XmlReflectionImporter(overrides, defaultNamespace);
     if (extraTypes != null)
     {
         for (int i = 0; i < extraTypes.Length; i++)
         {
             importer.IncludeType(extraTypes[i]);
         }
     }
     this.mapping = importer.ImportTypeMapping(type, root, defaultNamespace);
     if (location != null)
     {
         this.DemandForUserLocation();
     }
     this.tempAssembly = GenerateTempAssembly(this.mapping, type, defaultNamespace, location, evidence);
 }
Ejemplo n.º 30
0
 internal object InvokeReader(XmlMapping mapping, XmlReader xmlReader, XmlDeserializationEvents events, string encodingStyle) {
     XmlSerializationReader reader = null;
     try {
         encodingStyle = ValidateEncodingStyle(encodingStyle, mapping.Key);
         reader = Contract.Reader;
         reader.Init(xmlReader, events, encodingStyle, this);
         if (methods[mapping.Key].readMethod == null) {
             if (readerMethods == null) {
                 readerMethods = Contract.ReadMethods;
             }
             string methodName = (string)readerMethods[mapping.Key];
             if (methodName == null) {
                 throw new InvalidOperationException(Res.GetString(Res.XmlNotSerializable, mapping.Accessor.Name));
             }
             methods[mapping.Key].readMethod = GetMethodFromType(reader.GetType(), methodName, pregeneratedAssmbly ? this.assembly : null);
         }
         return methods[mapping.Key].readMethod.Invoke(reader, emptyObjectArray);
     }
     catch (SecurityException e) {
         throw new InvalidOperationException(Res.GetString(Res.XmlNoPartialTrust), e);
     }
     finally {
         if (reader != null)
             reader.Dispose();
     }
 }
 public object Deserialize(XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)
 {
     object obj2;
     events.sender = this;
     try
     {
         if (this.primitiveType != null)
         {
             if ((encodingStyle != null) && (encodingStyle.Length > 0))
             {
                 throw new InvalidOperationException(Res.GetString("XmlInvalidEncodingNotEncoded1", new object[] { encodingStyle }));
             }
             return this.DeserializePrimitive(xmlReader, events);
         }
         if ((this.tempAssembly == null) || this.typedSerializer)
         {
             XmlSerializationReader reader = this.CreateReader();
             reader.Init(xmlReader, events, encodingStyle, this.tempAssembly);
             try
             {
                 return this.Deserialize(reader);
             }
             finally
             {
                 reader.Dispose();
             }
         }
         obj2 = this.tempAssembly.InvokeReader(this.mapping, xmlReader, events, encodingStyle);
     }
     catch (Exception innerException)
     {
         if (((innerException is ThreadAbortException) || (innerException is StackOverflowException)) || (innerException is OutOfMemoryException))
         {
             throw;
         }
         if (innerException is TargetInvocationException)
         {
             innerException = innerException.InnerException;
         }
         if (xmlReader is IXmlLineInfo)
         {
             IXmlLineInfo info = (IXmlLineInfo) xmlReader;
             throw new InvalidOperationException(Res.GetString("XmlSerializeErrorDetails", new object[] { info.LineNumber.ToString(CultureInfo.InvariantCulture), info.LinePosition.ToString(CultureInfo.InvariantCulture) }), innerException);
         }
         throw new InvalidOperationException(Res.GetString("XmlSerializeError"), innerException);
     }
     return obj2;
 }
        // this method must be called before any generated deserialization methods are called
        internal void Init(XmlReader r, XmlDeserializationEvents events, string encodingStyle, TempAssembly tempAssembly) {
            this.events = events;
            if (checkDeserializeAdvances)
            {
                this.countingReader = new XmlCountingReader(r);
                this.r = this.countingReader;
            }
            else 
                this.r = r;
            this.d = null;
            this.soap12 = (encodingStyle == Soap12.Encoding);
            Init(tempAssembly);

            schemaNsID = r.NameTable.Add(XmlSchema.Namespace);
            schemaNs2000ID = r.NameTable.Add("http://www.w3.org/2000/10/XMLSchema");
            schemaNs1999ID = r.NameTable.Add("http://www.w3.org/1999/XMLSchema");
            schemaNonXsdTypesNsID = r.NameTable.Add(UrtTypes.Namespace);
            instanceNsID = r.NameTable.Add(XmlSchema.InstanceNamespace);
            instanceNs2000ID = r.NameTable.Add("http://www.w3.org/2000/10/XMLSchema-instance");
            instanceNs1999ID = r.NameTable.Add("http://www.w3.org/1999/XMLSchema-instance");
            soapNsID = r.NameTable.Add(Soap.Encoding);
            soap12NsID = r.NameTable.Add(Soap12.Encoding);
            schemaID = r.NameTable.Add("schema");
            wsdlNsID = r.NameTable.Add(Wsdl.Namespace);
            wsdlArrayTypeID = r.NameTable.Add(Wsdl.ArrayType);
            nullID = r.NameTable.Add("null");
            nilID = r.NameTable.Add("nil");
            typeID = r.NameTable.Add("type");
            arrayTypeID = r.NameTable.Add("arrayType");
            itemTypeID = r.NameTable.Add("itemType");
            arraySizeID = r.NameTable.Add("arraySize");
            arrayID = r.NameTable.Add("Array");
            urTypeID = r.NameTable.Add(Soap.UrType);
            InitIDs();
        }
        private object DeserializePrimitive(XmlReader xmlReader, XmlDeserializationEvents events)
        {
            XmlSerializationPrimitiveReader reader = new XmlSerializationPrimitiveReader();
            reader.Init(xmlReader, events, null, null);
            switch (Type.GetTypeCode(this.primitiveType))
            {
                case TypeCode.Boolean:
                    return reader.Read_boolean();

                case TypeCode.Char:
                    return reader.Read_char();

                case TypeCode.SByte:
                    return reader.Read_byte();

                case TypeCode.Byte:
                    return reader.Read_unsignedByte();

                case TypeCode.Int16:
                    return reader.Read_short();

                case TypeCode.UInt16:
                    return reader.Read_unsignedShort();

                case TypeCode.Int32:
                    return reader.Read_int();

                case TypeCode.UInt32:
                    return reader.Read_unsignedInt();

                case TypeCode.Int64:
                    return reader.Read_long();

                case TypeCode.UInt64:
                    return reader.Read_unsignedLong();

                case TypeCode.Single:
                    return reader.Read_float();

                case TypeCode.Double:
                    return reader.Read_double();

                case TypeCode.Decimal:
                    return reader.Read_decimal();

                case TypeCode.DateTime:
                    return reader.Read_dateTime();

                case TypeCode.String:
                    return reader.Read_string();
            }
            if (this.primitiveType == typeof(XmlQualifiedName))
            {
                return reader.Read_QName();
            }
            if (this.primitiveType == typeof(byte[]))
            {
                return reader.Read_base64Binary();
            }
            if (this.primitiveType != typeof(Guid))
            {
                throw new InvalidOperationException(Res.GetString("XmlUnxpectedType", new object[] { this.primitiveType.FullName }));
            }
            return reader.Read_guid();
        }