Exemple #1
0
        protected void WriteTypedPrimitive(string name, string ns, object o, bool xsiType)
        {
            string   value;
            TypeData td = TypeTranslator.GetTypeData(o.GetType());

            if (td.SchemaType != SchemaTypes.Primitive)
            {
                throw new InvalidOperationException(String.Format("The type of the argument object '{0}' is not primitive.", td.FullTypeName));
            }

            if (name == null)
            {
                ns   = td.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace;
                name = td.XmlType;
            }
            else
            {
                name = XmlCustomFormatter.FromXmlName(name);
            }
            Writer.WriteStartElement(name, ns);

            if (o is XmlQualifiedName)
            {
                value = FromXmlQualifiedName((XmlQualifiedName)o);
            }
            else
            {
                value = XmlCustomFormatter.ToXmlString(td, o);
            }

            if (xsiType)
            {
                if (td.SchemaType != SchemaTypes.Primitive)
                {
                    throw new InvalidOperationException(string.Format(unexpectedTypeError, o.GetType().FullName));
                }
                WriteXsiType(td.XmlType, td.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace);
            }

            WriteValue(value);

            Writer.WriteEndElement();
        }
Exemple #2
0
        void WriteStartElement(string name, string ns, object o, bool writePrefixed, ICollection namespaces)
        {
            if (o != null)
            {
                if (serializedObjects.Contains(o))
                {
                    throw new InvalidOperationException("A circular reference was detected while serializing an object of type " + o.GetType().Name);
                }
                else
                {
                    serializedObjects [o] = o;
                }
            }

            string prefix = null;

            if (topLevelElement && ns != null && ns.Length != 0)
            {
                foreach (XmlQualifiedName qn in namespaces)
                {
                    if (qn.Namespace == ns)
                    {
                        prefix        = qn.Name;
                        writePrefixed = true;
                        break;
                    }
                }
            }

            if (writePrefixed && ns != string.Empty)
            {
                name = XmlCustomFormatter.FromXmlName(name);

                if (prefix == null)
                {
                    prefix = Writer.LookupPrefix(ns);
                }
                if (prefix == null || prefix.Length == 0)
                {
                    prefix = "q" + (++qnameCount);
                }
                Writer.WriteStartElement(prefix, name, ns);
            }
            else
            {
                Writer.WriteStartElement(name, ns);
            }

            if (topLevelElement)
            {
                if (namespaces != null)
                {
                    foreach (XmlQualifiedName qn in namespaces)
                    {
                        string currentPrefix = Writer.LookupPrefix(qn.Namespace);
                        if (currentPrefix != null && currentPrefix.Length != 0)
                        {
                            continue;
                        }

                        WriteAttribute("xmlns", qn.Name, xmlNamespace, qn.Namespace);
                    }
                }
                topLevelElement = false;
            }
        }
Exemple #3
0
 protected void WriteEmptyTag(string name, string ns)
 {
     name = XmlCustomFormatter.FromXmlName(name);
     WriteStartElement(name, ns);
     WriteEndElement();
 }
Exemple #4
0
 protected static string FromXmlName(string name)
 {
     return(XmlCustomFormatter.FromXmlName(name));
 }