Example #1
0
            private void WriteFaultCodeElement(XmlDictionaryWriter writer, FaultCode code)
            {
                var prefix = writer.LookupPrefix(_constants.EnvelopeNamespace);

                if (!string.IsNullOrEmpty(code.Namespace) && code.Namespace != _constants.EnvelopeNamespace)
                {
                    prefix = writer.LookupPrefix(code.Namespace) ?? "custom";
                }

                var name = code.Name;

                writer.WriteStartElement("faultcode");

                if (!code.IsPredefinedFault)
                {
                    if (prefix == "custom")
                    {
                        writer.WriteXmlnsAttribute(prefix, code.Namespace);
                    }
                }
                else
                {
                    if (code.IsReceiverFault)
                    {
                        name = "Server";
                    }
                    if (code.IsSenderFault)
                    {
                        name = "Client";
                    }
                }

                writer.WriteString($"{prefix}:{name}");
                writer.WriteEndElement();
            }
Example #2
0
 private void WriteFaultCode(XmlDictionaryWriter writer,
                             EnvelopeVersion version, FaultCode code, bool sub)
 {
     if (version == EnvelopeVersion.Soap11)
     {
         writer.WriteStartElement("", "faultcode", String.Empty);
         if (code.Namespace.Length > 0 && String.IsNullOrEmpty(writer.LookupPrefix(code.Namespace)))
         {
             writer.WriteXmlnsAttribute("a", code.Namespace);
         }
         writer.WriteQualifiedName(code.Name, code.Namespace);
         writer.WriteEndElement();
     }
     else                 // Soap12
     {
         writer.WriteStartElement(sub ? "Subcode" : "Code", version.Namespace);
         writer.WriteStartElement("Value", version.Namespace);
         if (code.Namespace.Length > 0 && String.IsNullOrEmpty(writer.LookupPrefix(code.Namespace)))
         {
             writer.WriteXmlnsAttribute("a", code.Namespace);
         }
         writer.WriteQualifiedName(code.Name, code.Namespace);
         writer.WriteEndElement();
         if (code.SubCode != null)
         {
             WriteFaultCode(writer, version, code.SubCode, true);
         }
         writer.WriteEndElement();
     }
 }
Example #3
0
        /// <summary>
        /// Called when the message body is written to an XML file.
        /// </summary>
        /// <param name="writer">A <see cref="T:System.Xml.XmlDictionaryWriter"/> that is used to write this message body to an XML file.</param>
        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            try
            {
                var prefix = writer.LookupPrefix(this._xmlQualifiedName.Namespace) ?? "web";
                this.ActionQueue.Enqueue(() => writer.WriteStartElement(prefix, this._xmlQualifiedName.Name, this._xmlQualifiedName.Namespace));

                base.OnWriteBodyContents(writer);
                writer.WriteEndElement();
            }
            catch (FaultException e)
            {
                var messageFault = new SdmxMessageFault(e.CreateMessageFault(), this._xmlQualifiedName.Namespace);
                messageFault.WriteTo(writer, this.Version.Envelope);
                if (WebOperationContext.Current != null)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.InternalServerError;
                }

                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Response.StatusCode = 500;
                }

                this._isFaulted = true;
            }
            finally
            {
                writer.Flush();
            }
        }
Example #4
0
        private void WriteSoap12Fault(XmlDictionaryWriter writer)
        {
            // NOTE: This default culture is a hack until a better localisation solution is
            // built. At this stage it assumes the current thread culture
            var defaultCulture = CultureInfo.CurrentCulture;

            var faultString = _faultStringOverride ?? (_exception.InnerException != null ? _exception.InnerException.Message : _exception.Message);
            var faultDetail = ExtractFaultDetailsAsXmlElement(_exception);
            var prefix      = writer.LookupPrefix(Soap12Namespace) ?? "s";

            writer.WriteStartElement(prefix, "Fault", Soap12Namespace);

            writer.WriteStartElement(prefix, "Code", Soap12Namespace);
            writer.WriteStartElement(prefix, "Value", Soap12Namespace);
            writer.WriteString(prefix + ":Sender");
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteStartElement(prefix, "Reason", Soap12Namespace);
            writer.WriteStartElement(prefix, "Text", Soap12Namespace);
            writer.WriteAttributeString("xml:lang", defaultCulture.IetfLanguageTag);
            writer.WriteString(faultString);

            writer.WriteEndElement();
            writer.WriteEndElement();

            if (faultDetail != null)
            {
                writer.WriteStartElement(prefix, "Detail", Soap12Namespace);
                faultDetail.WriteTo(writer);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }
Example #5
0
 protected virtual void AddPrefixIfNeeded(XmlDictionaryWriter writer, string prefix, string ns)
 {
     if (writer.LookupPrefix(ns) == null)
     {
         writer.WriteXmlnsAttribute(prefix, ns);
     }
 }
        private void WriteTo11(XmlDictionaryWriter writer)
        {
            string name;
            string str2;

            writer.WriteStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace);
            writer.WriteStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace);
            FaultCode subCode = this.Code;

            if (subCode.SubCode != null)
            {
                subCode = subCode.SubCode;
            }
            if (subCode.IsSenderFault)
            {
                name = "Client";
            }
            else if (subCode.IsReceiverFault)
            {
                name = "Server";
            }
            else
            {
                name = subCode.Name;
            }
            if (subCode.IsPredefinedFault)
            {
                str2 = "http://schemas.xmlsoap.org/soap/envelope/";
            }
            else
            {
                str2 = subCode.Namespace;
            }
            if (writer.LookupPrefix(str2) == null)
            {
                writer.WriteAttributeString("xmlns", "a", "http://www.w3.org/2000/xmlns/", str2);
            }
            writer.WriteQualifiedName(name, str2);
            writer.WriteEndElement();
            FaultReasonText text = this.Reason.Translations[0];

            writer.WriteStartElement(XD.Message11Dictionary.FaultString, XD.Message11Dictionary.FaultNamespace);
            if (text.XmlLang.Length > 0)
            {
                writer.WriteAttributeString("xml", "lang", "http://www.w3.org/XML/1998/namespace", text.XmlLang);
            }
            writer.WriteString(text.Text);
            writer.WriteEndElement();
            if (this.Actor.Length > 0)
            {
                writer.WriteElementString(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace, this.Actor);
            }
            if (this.HasDetail)
            {
                this.OnWriteDetail(writer, EnvelopeVersion.Soap11);
            }
            writer.WriteEndElement();
        }
Example #7
0
        public void Serialize(Type type, object graph)
        {
            if (graph == null)
            {
                writer.WriteAttributeString("nil", XmlSchema.InstanceNamespace, "true");
            }
            else
            {
                Type actualType = graph.GetType();

                SerializationMap map = types.FindUserMap(actualType);
                // For some collection types, the actual type does not matter. So get nominal serialization type instead.
                // (The code below also covers the lines above, but I don't remove above lines to avoid extra search cost.)
                if (map == null)
                {
                    actualType = types.GetSerializedType(actualType);
                    map        = types.FindUserMap(actualType);
                }
                // If it is still unknown, then register it.
                if (map == null)
                {
                    types.Add(actualType);
                    map = types.FindUserMap(actualType);
                }

                if (actualType != type && (map == null || map.OutputXsiType))
                {
                    QName  qname = types.GetXmlName(actualType);
                    string name  = qname.Name;
                    string ns    = qname.Namespace;
                    if (qname == QName.Empty)
                    {
                        name = XmlConvert.EncodeLocalName(actualType.Name);
                        ns   = KnownTypeCollection.DefaultClrNamespaceBase + actualType.Namespace;
                    }
                    else if (qname.Namespace == KnownTypeCollection.MSSimpleNamespace)
                    {
                        ns = XmlSchema.Namespace;
                    }
                    if (writer.LookupPrefix(ns) == null)                      // it goes first (extraneous, but it makes att order compatible)
                    {
                        writer.WriteXmlnsAttribute(null, ns);
                    }
                    writer.WriteStartAttribute("type", XmlSchema.InstanceNamespace);
                    writer.WriteQualifiedName(name, ns);
                    writer.WriteEndAttribute();
                }
                QName predef = KnownTypeCollection.GetPredefinedTypeName(actualType);
                if (predef != QName.Empty)
                {
                    SerializePrimitive(type, graph, predef);
                }
                else
                {
                    map.Serialize(graph, this);
                }
            }
        }
Example #8
0
        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            writer.WriteStartElement("BaseObjectSearchRequest", "http://schemas.microsoft.com/2006/11/IdentityManagement/DirectoryAccess");
            writer.WriteAttributeString("Dialect", "http://schemas.microsoft.com/2008/1/ActiveDirectory/Dialect/XPath-Level-1");
            base.OnWriteBodyContents(writer);
            string str  = writer.LookupPrefix("http://schemas.microsoft.com/2008/1/ActiveDirectory/Data");
            string str1 = writer.LookupPrefix("http://schemas.microsoft.com/2008/1/ActiveDirectory");

            if (this._attributeList != null)
            {
                XmlUtility.SerializeAttributeList(writer, "AttributeType", "http://schemas.microsoft.com/2006/11/IdentityManagement/DirectoryAccess", str1, str, this._attributeList);
            }
            if (this._controls != null)
            {
                DirectoryControlSerializer.Serialize(writer, this._controls);
            }
            writer.WriteEndElement();
        }
Example #9
0
        internal static void AddNamespaceDeclaration(XmlDictionaryWriter writer, string prefix, XmlDictionaryString ns)
        {
            string p = writer.LookupPrefix(ns.Value);

            if (p == null || p != prefix)
            {
                writer.WriteXmlnsAttribute(prefix, ns);
            }
        }
Example #10
0
        public static void WriteXsiTypeAttribute(XmlDictionaryWriter writer, string xsdType)
        {
            string str = writer.LookupPrefix("http://www.w3.org/2001/XMLSchema");

            object[] objArray = new object[2];
            objArray[0] = str;
            objArray[1] = xsdType;
            writer.WriteAttributeString("type", "http://www.w3.org/2001/XMLSchema-instance", string.Format(CultureInfo.CurrentCulture, "{0}:{1}", objArray));
        }
        internal static string EnsureNamespaceDefined(XmlDictionaryWriter writer, XmlDictionaryString ns, string defaultPrefix)
        {
            string prefix = writer.LookupPrefix(ns.Value);

            if (prefix == null)
            {
                writer.WriteXmlnsAttribute(defaultPrefix, ns);
                prefix = defaultPrefix;
            }
            return(prefix);
        }
Example #12
0
 protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer)
 {
     base.OnWriteStartEnvelope(writer);
     writer.WriteXmlnsAttribute("addata", "http://schemas.microsoft.com/2008/1/ActiveDirectory/Data");
     writer.WriteXmlnsAttribute("ad", "http://schemas.microsoft.com/2008/1/ActiveDirectory");
     writer.WriteXmlnsAttribute("xsd", "http://www.w3.org/2001/XMLSchema");
     writer.WriteXmlnsAttribute("xsi", "http://www.w3.org/2001/XMLSchema-instance");
     if (writer.LookupPrefix("http://www.w3.org/2005/08/addressing") == null)
     {
         writer.WriteXmlnsAttribute("wsa", "http://www.w3.org/2005/08/addressing");
     }
 }
        protected virtual void WriteWebServiceDescriptorTypeAttribute(XmlDictionaryWriter writer, WebServiceDescriptor webServiceDescriptor)
        {
            var prefix = writer.LookupPrefix(Namespace);

            if (string.IsNullOrEmpty(prefix))
            {
                // this is added for testing purposes
                writer.WriteXmlnsAttribute(Prefix, Namespace);
                prefix = Prefix;
            }
            var type = $"{prefix}:{webServiceDescriptor.GetType().Name.Replace("Descriptor", string.Empty)}Type";

            writer.WriteAttributeString(XsiConstants.Prefix, XsiConstants.Attributes.Type, XsiConstants.Namespace, type);
        }
Example #14
0
        protected virtual void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion version)
        {
            var dic = Constants.SoapDictionary;
            XmlDictionaryString name, ns;
            var prefix = writer.LookupPrefix(Namespace);

            if (dic.TryLookup(Name, out name) && dic.TryLookup(Namespace, out ns))
            {
                writer.WriteStartElement(prefix, name, ns);
            }
            else
            {
                writer.WriteStartElement(prefix, this.Name, this.Namespace);
            }
            WriteHeaderAttributes(writer, version);
        }
        public virtual void WriteXml(XmlDictionaryWriter writer, SamlSerializer samlSerializer, SecurityTokenSerializer keyInfoSerializer)
        {
            CheckObjectValidity();

            if (writer == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("writer"));
            }

            if (samlSerializer == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("samlSerializer"));
            }

#pragma warning suppress 56506 // samlSerializer.DictionaryManager is never null.
            SamlDictionary dictionary = samlSerializer.DictionaryManager.SamlDictionary;

            writer.WriteStartElement(dictionary.PreferredPrefix.Value, dictionary.AuthorityBinding, dictionary.Namespace);

            string prefix = null;
            if (!string.IsNullOrEmpty(this.authorityKind.Namespace))
            {
                writer.WriteAttributeString(String.Empty, dictionary.NamespaceAttributePrefix.Value, null, this.authorityKind.Namespace);
                prefix = writer.LookupPrefix(this.authorityKind.Namespace);
            }

            writer.WriteStartAttribute(dictionary.AuthorityKind, null);
            if (string.IsNullOrEmpty(prefix))
            {
                writer.WriteString(this.authorityKind.Name);
            }
            else
            {
                writer.WriteString(prefix + ":" + this.authorityKind.Name);
            }
            writer.WriteEndAttribute();

            writer.WriteStartAttribute(dictionary.Location, null);
            writer.WriteString(this.location);
            writer.WriteEndAttribute();

            writer.WriteStartAttribute(dictionary.Binding, null);
            writer.WriteString(this.binding);
            writer.WriteEndAttribute();

            writer.WriteEndElement();
        }
Example #16
0
        private void WriteFaultCode12Driver(XmlDictionaryWriter writer, FaultCode faultCode, EnvelopeVersion version)
        {
            writer.WriteStartElement(XD.Message12Dictionary.FaultValue, version.DictionaryNamespace);
            string name;

            if (faultCode.IsSenderFault)
            {
                name = version.SenderFaultName;
            }
            else if (faultCode.IsReceiverFault)
            {
                name = version.ReceiverFaultName;
            }
            else
            {
                name = faultCode.Name;
            }

            string ns;

            if (faultCode.IsPredefinedFault)
            {
                ns = version.Namespace;
            }
            else
            {
                ns = faultCode.Namespace;
            }

            string prefix = writer.LookupPrefix(ns);

            if (prefix == null)
            {
                writer.WriteAttributeString("xmlns", "a", XmlUtil.XmlNsNs, ns);
            }

            writer.WriteQualifiedName(name, ns);
            writer.WriteEndElement();

            if (faultCode.SubCode != null)
            {
                writer.WriteStartElement(XD.Message12Dictionary.FaultSubcode, version.DictionaryNamespace);
                WriteFaultCode12Driver(writer, faultCode.SubCode, version);
                writer.WriteEndElement();
            }
        }
Example #17
0
        protected virtual void OnWriteStartEnvelope(
            XmlDictionaryWriter writer)
        {
            var dic = Constants.SoapDictionary;

            writer.WriteStartElement("s", dic.Add("Envelope"), dic.Add(Version.Envelope.Namespace));
            if (Headers.Action != null && Version.Addressing.Namespace != MessageVersion.None.Addressing.Namespace)
            {
                writer.WriteXmlnsAttribute("a", dic.Add(Version.Addressing.Namespace));
            }
            foreach (MessageHeaderInfo h in Headers)
            {
                if (h.Id != null && writer.LookupPrefix(Constants.WsuNamespace) != "u")
                {
                    writer.WriteXmlnsAttribute("u", dic.Add(Constants.WsuNamespace));
                    break;
                }
            }
        }
        private void WriteFaultCode12Driver(XmlDictionaryWriter writer, FaultCode faultCode, EnvelopeVersion version)
        {
            string senderFaultName;
            string str2;

            writer.WriteStartElement(XD.Message12Dictionary.FaultValue, version.DictionaryNamespace);
            if (faultCode.IsSenderFault)
            {
                senderFaultName = version.SenderFaultName;
            }
            else if (faultCode.IsReceiverFault)
            {
                senderFaultName = version.ReceiverFaultName;
            }
            else
            {
                senderFaultName = faultCode.Name;
            }
            if (faultCode.IsPredefinedFault)
            {
                str2 = version.Namespace;
            }
            else
            {
                str2 = faultCode.Namespace;
            }
            if (writer.LookupPrefix(str2) == null)
            {
                writer.WriteAttributeString("xmlns", "a", "http://www.w3.org/2000/xmlns/", str2);
            }
            writer.WriteQualifiedName(senderFaultName, str2);
            writer.WriteEndElement();
            if (faultCode.SubCode != null)
            {
                writer.WriteStartElement(XD.Message12Dictionary.FaultSubcode, version.DictionaryNamespace);
                this.WriteFaultCode12Driver(writer, faultCode.SubCode, version);
                writer.WriteEndElement();
            }
        }
Example #19
0
        /// <summary>
        /// When overridden in a non-abstract derived class, writes the contents of the detail element.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Xml.XmlDictionaryWriter"/> used to write the detail element.</param>
        protected override void OnWriteDetailContents(XmlDictionaryWriter writer)
        {
            var prefix = writer.LookupPrefix(this._nameSpace) ?? "web";

            using (var reader = this._messageFault.GetReaderAtDetailContents())
            {
                if ("Error".Equals(reader.LocalName) && string.IsNullOrWhiteSpace(reader.NamespaceURI))
                {
                    writer.WriteStartElement(prefix, "Error", this._nameSpace);
                    reader.Read();

                    // ErrorMessage
                    writer.WriteNode(reader, false);

                    // ErrorNumber
                    writer.WriteNode(reader, false);

                    //
                    writer.WriteNode(reader, false);
                }
            }
        }
Example #20
0
        private void WriteTo11(XmlDictionaryWriter writer)
        {
            writer.WriteStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace);
            writer.WriteStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace);

            FaultCode faultCode = Code;

            if (faultCode.SubCode != null)
            {
                faultCode = faultCode.SubCode;
            }

            string name;

            if (faultCode.IsSenderFault)
            {
                name = "Client";
            }
            else if (faultCode.IsReceiverFault)
            {
                name = "Server";
            }
            else
            {
                name = faultCode.Name;
            }

            string ns;

            if (faultCode.IsPredefinedFault)
            {
                ns = Message11Strings.Namespace;
            }
            else
            {
                ns = faultCode.Namespace;
            }

            string prefix = writer.LookupPrefix(ns);

            if (prefix == null)
            {
                writer.WriteAttributeString("xmlns", "a", XmlUtil.XmlNsNs, ns);
            }

            writer.WriteQualifiedName(name, ns);
            writer.WriteEndElement();
            FaultReasonText translation = Reason.Translations[0];

            writer.WriteStartElement(XD.Message11Dictionary.FaultString, XD.Message11Dictionary.FaultNamespace);
            if (translation.XmlLang.Length > 0)
            {
                writer.WriteAttributeString("xml", "lang", XmlUtil.XmlNs, translation.XmlLang);
            }

            writer.WriteString(translation.Text);
            writer.WriteEndElement();
            if (Actor.Length > 0)
            {
                writer.WriteElementString(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace, Actor);
            }

            if (HasDetail)
            {
                OnWriteDetail(writer, EnvelopeVersion.Soap11);
            }
            writer.WriteEndElement();
        }
        public void LookupPrefix()
        {
            xw.WriteStartElement("root");

            xw.WriteStartElement("one");
            xw.WriteAttributeString("xmlns", "foo", null, "http://abc.def");
            xw.WriteAttributeString("xmlns", "bar", null, "http://ghi.jkl");
            Assert.AreEqual("foo", xw.LookupPrefix("http://abc.def"));
            Assert.AreEqual("bar", xw.LookupPrefix("http://ghi.jkl"));
            xw.WriteEndElement();

            xw.WriteStartElement("two");
            xw.WriteAttributeString("xmlns", "baz", null, "http://mno.pqr");
            xw.WriteString("quux");
            Assert.AreEqual("baz", xw.LookupPrefix("http://mno.pqr"));
            Assert.IsNull(xw.LookupPrefix("http://abc.def"));
            Assert.IsNull(xw.LookupPrefix("http://ghi.jkl"));

            Assert.IsNull(xw.LookupPrefix("http://bogus"));
        }
Example #22
0
        private static void InternalSerialize(XmlDictionaryWriter writer, ChangeOperation ChangeOperation, string ns, string property, object value)
        {
            string str = writer.LookupPrefix(ns);

            writer.LookupPrefix("http://schemas.microsoft.com/2008/1/ActiveDirectory");
            if (ChangeOperation != ChangeOperation.None)
            {
                writer.WriteStartElement("Change", "http://schemas.microsoft.com/2006/11/IdentityManagement/DirectoryAccess");
                ChangeOperation changeOperation = ChangeOperation;
                switch (changeOperation)
                {
                case ChangeOperation.Add:
                {
                    writer.WriteAttributeString("Operation", "add");
                    break;
                }

                case ChangeOperation.Delete:
                {
                    writer.WriteAttributeString("Operation", "delete");
                    break;
                }

                case ChangeOperation.Replace:
                {
                    writer.WriteAttributeString("Operation", "replace");
                    break;
                }
                }
            }
            else
            {
                writer.WriteStartElement("AttributeTypeAndValue", "http://schemas.microsoft.com/2006/11/IdentityManagement/DirectoryAccess");
            }
            writer.WriteElementString("AttributeType", "http://schemas.microsoft.com/2006/11/IdentityManagement/DirectoryAccess", AttributeTypeAndValueSerializer.FormatAttributeName(str, property));
            if (value != null)
            {
                if (value as ICollection == null)
                {
                    writer.WriteStartElement("AttributeValue", "http://schemas.microsoft.com/2006/11/IdentityManagement/DirectoryAccess");
                    if (value as DirectoryAttributeModification == null)
                    {
                        ADValueSerializer.Serialize(writer, value);
                    }
                    else
                    {
                        DirectoryAttributeModification directoryAttributeModification = (DirectoryAttributeModification)value;
                        ADValueSerializer.Serialize(writer, directoryAttributeModification[0]);
                    }
                    writer.WriteEndElement();
                }
                else
                {
                    ICollection collections = (ICollection)value;
                    if (collections.Count > 0)
                    {
                        writer.WriteStartElement("AttributeValue", "http://schemas.microsoft.com/2006/11/IdentityManagement/DirectoryAccess");
                        foreach (object obj in collections)
                        {
                            ADValueSerializer.Serialize(writer, obj);
                        }
                        writer.WriteEndElement();
                    }
                }
            }
            writer.WriteEndElement();
        }
Example #23
0
 public void LookupPrefixNull()
 {
     w.LookupPrefix(null);
 }
Example #24
0
        public virtual void WriteEndpointReferenceCollection(XmlDictionaryWriter writer, string name, string ns, ICollection <EndpointReference> endpointReferenceCollection)
        {
            var prefix = writer.LookupPrefix(ns);

            WriteEndpointReferenceCollection(writer, prefix, name, ns, endpointReferenceCollection);
        }
Example #25
0
        public void Serialize(Type type, object graph)
        {
            if (graph == null)
            {
                writer.WriteAttributeString("nil", XmlSchema.InstanceNamespace, "true");
            }
#if !MOONLIGHT
            else if (type == typeof(XmlElement))
            {
                ((XmlElement)graph).WriteTo(Writer);
            }
            else if (type == typeof(XmlNode []))
            {
                foreach (var xn in (XmlNode [])graph)
                {
                    xn.WriteTo(Writer);
                }
            }
#endif
            else
            {
                QName resolvedQName = null;
                if (resolver != null)
                {
                    XmlDictionaryString rname, rns;
                    if (resolver.TryResolveType(graph != null ? graph.GetType() : typeof(object), type, default_resolver, out rname, out rns))
                    {
                        resolvedQName = new QName(rname.Value, rns.Value);
                    }
                }

                Type actualType = graph.GetType();

                SerializationMap map;
                map = types.FindUserMap(actualType);
                // For some collection types, the actual type does not matter. So get nominal serialization type instead.
                // (The code below also covers the lines above, but I don't remove above lines to avoid extra search cost.)
                if (map == null)
                {
                    // FIXME: not sure if type.IsInterface is the correct condition to determine whether items are serialized with i:type or not. (e.g. bug #675144 server response).
                    actualType = types.GetSerializedType(type.IsInterface ? type : actualType);
                    map        = types.FindUserMap(actualType);
                }
                // If it is still unknown, then register it.
                if (map == null)
                {
                    types.Add(actualType);
                    map = types.FindUserMap(actualType);
                }

                if (actualType != type && (map == null || map.OutputXsiType))
                {
                    QName  qname = resolvedQName ?? types.GetXmlName(actualType);
                    string name  = qname.Name;
                    string ns    = qname.Namespace;
                    if (qname == QName.Empty)
                    {
                        name = XmlConvert.EncodeLocalName(actualType.Name);
                        ns   = KnownTypeCollection.DefaultClrNamespaceBase + actualType.Namespace;
                    }
                    else if (qname.Namespace == KnownTypeCollection.MSSimpleNamespace)
                    {
                        ns = XmlSchema.Namespace;
                    }
                    if (writer.LookupPrefix(ns) == null)                      // it goes first (extraneous, but it makes att order compatible)
                    {
                        writer.WriteXmlnsAttribute(null, ns);
                    }
                    writer.WriteStartAttribute("i", "type", XmlSchema.InstanceNamespace);
                    writer.WriteQualifiedName(name, ns);
                    writer.WriteEndAttribute();
                }
                QName predef = KnownTypeCollection.GetPredefinedTypeName(actualType);
                if (predef != QName.Empty)
                {
                    SerializePrimitive(type, graph, predef);
                }
                else
                {
                    map.Serialize(graph, this);
                }
            }
        }
 public override string LookupPrefix(string ns)
 {
     return(_innerWriter.LookupPrefix(ns));
 }
Example #27
0
        public static void Serialize(XmlDictionaryWriter writer, object value)
        {
            string str;

            byte[] numArray;
            object obj;
            bool   flag  = false;
            bool   flag1 = false;

            writer.LookupPrefix("http://www.w3.org/2001/XMLSchema");
            writer.WriteStartElement("value", "http://schemas.microsoft.com/2008/1/ActiveDirectory");
            if (value.GetType() != typeof(byte[]))
            {
                TypeCode typeCode = Type.GetTypeCode(value.GetType());
                if (typeCode == TypeCode.Boolean)
                {
                    str  = "boolean";
                    flag = false;
                    if ((bool)value)
                    {
                        obj = "true";
                    }
                    else
                    {
                        obj = "false";
                    }
                    value = obj;
                    XmlUtility.WriteXsiTypeAttribute(writer, str);
                    if (!flag)
                    {
                        writer.WriteString(value.ToString());
                    }
                    else
                    {
                        if (!flag1)
                        {
                            numArray    = new byte[1];
                            numArray[0] = (byte)value;
                        }
                        else
                        {
                            numArray = (byte[])value;
                        }
                        writer.WriteBase64(numArray, 0, (int)numArray.Length);
                    }
                    writer.WriteEndElement();
                    return;
                }
                else if (typeCode == TypeCode.Char || typeCode == TypeCode.SByte || typeCode == TypeCode.Int16 || typeCode == TypeCode.UInt16 || typeCode == TypeCode.Int32 || typeCode == TypeCode.UInt32)
                {
                    str  = "int";
                    flag = false;
                    XmlUtility.WriteXsiTypeAttribute(writer, str);
                    if (!flag)
                    {
                        writer.WriteString(value.ToString());
                    }
                    else
                    {
                        if (!flag1)
                        {
                            numArray    = new byte[1];
                            numArray[0] = (byte)value;
                        }
                        else
                        {
                            numArray = (byte[])value;
                        }
                        writer.WriteBase64(numArray, 0, (int)numArray.Length);
                    }
                    writer.WriteEndElement();
                    return;
                }
                else if (typeCode == TypeCode.Byte)
                {
                    str   = "base64Binary";
                    flag  = true;
                    flag1 = false;
                    XmlUtility.WriteXsiTypeAttribute(writer, str);
                    if (!flag)
                    {
                        writer.WriteString(value.ToString());
                    }
                    else
                    {
                        if (!flag1)
                        {
                            numArray    = new byte[1];
                            numArray[0] = (byte)value;
                        }
                        else
                        {
                            numArray = (byte[])value;
                        }
                        writer.WriteBase64(numArray, 0, (int)numArray.Length);
                    }
                    writer.WriteEndElement();
                    return;
                }
                else if (typeCode == TypeCode.Int64 || typeCode == TypeCode.UInt64)
                {
                    str  = "long";
                    flag = false;
                    XmlUtility.WriteXsiTypeAttribute(writer, str);
                    if (!flag)
                    {
                        writer.WriteString(value.ToString());
                    }
                    else
                    {
                        if (!flag1)
                        {
                            numArray    = new byte[1];
                            numArray[0] = (byte)value;
                        }
                        else
                        {
                            numArray = (byte[])value;
                        }
                        writer.WriteBase64(numArray, 0, (int)numArray.Length);
                    }
                    writer.WriteEndElement();
                    return;
                }
                else if (typeCode == TypeCode.Single || typeCode == TypeCode.Double || typeCode == TypeCode.Decimal || typeCode == (TypeCode.Object | TypeCode.DateTime))
                {
                }
                else if (typeCode == TypeCode.DateTime)
                {
                    str   = "dateTime";
                    flag  = false;
                    value = XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.Utc);
                    XmlUtility.WriteXsiTypeAttribute(writer, str);
                    if (!flag)
                    {
                        writer.WriteString(value.ToString());
                    }
                    else
                    {
                        if (!flag1)
                        {
                            numArray    = new byte[1];
                            numArray[0] = (byte)value;
                        }
                        else
                        {
                            numArray = (byte[])value;
                        }
                        writer.WriteBase64(numArray, 0, (int)numArray.Length);
                    }
                    writer.WriteEndElement();
                    return;
                }
                else if (typeCode == TypeCode.String)
                {
                    str  = "string";
                    flag = false;
                    XmlUtility.WriteXsiTypeAttribute(writer, str);
                    if (!flag)
                    {
                        writer.WriteString(value.ToString());
                    }
                    else
                    {
                        if (!flag1)
                        {
                            numArray    = new byte[1];
                            numArray[0] = (byte)value;
                        }
                        else
                        {
                            numArray = (byte[])value;
                        }
                        writer.WriteBase64(numArray, 0, (int)numArray.Length);
                    }
                    writer.WriteEndElement();
                    return;
                }
                object[] objArray = new object[1];
                objArray[0] = value.GetType().ToString();
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, StringResources.ADWSXmlParserUnexpectedElement, objArray));
            }
            else
            {
                flag  = true;
                flag1 = true;
                str   = "base64Binary";
            }
            XmlUtility.WriteXsiTypeAttribute(writer, str);
            if (!flag)
            {
                writer.WriteString(value.ToString());
            }
            else
            {
                if (!flag1)
                {
                    numArray    = new byte[1];
                    numArray[0] = (byte)value;
                }
                else
                {
                    numArray = (byte[])value;
                }
                writer.WriteBase64(numArray, 0, (int)numArray.Length);
            }
            writer.WriteEndElement();
        }