Exemple #1
0
        public void Write(Mono.System.Xml.XmlWriter writer, Mono.System.Xml.XmlNamespaceManager namespaceManager)
        {
            XmlSerializerNamespaces nss = new XmlSerializerNamespaces();

            if (namespaceManager != null)
            {
                foreach (string name in namespaceManager)
                {
                    //xml and xmlns namespaces are added by default in namespaceManager.
                    //So we should ignore them
                    if (name != "xml" && name != "xmlns")
                    {
                        nss.Add(name, namespaceManager.LookupNamespace(name));
                    }
                }
            }

            if (Namespaces != null && Namespaces.Count > 0)
            {
                XmlQualifiedName [] qnames = Namespaces.ToArray();
                foreach (XmlQualifiedName qn in qnames)
                {
                    nss.Add(qn.Name, qn.Namespace);
                }
                string p    = String.Empty;
                bool   loop = true;
                for (int idx = 1; loop; idx++)
                {
                    loop = false;
                    foreach (XmlQualifiedName qn in qnames)
                    {
                        if (qn.Name == p)
                        {
                            p    = "q" + idx;
                            loop = true;
                            break;
                        }
                    }
                }
                nss.Add(p, XmlSchema.Namespace);
            }

            if (nss.Count == 0)
            {
                // Add the xml schema namespace. (It is done
                // only when no entry exists in Namespaces).
                nss.Add("xs", XmlSchema.Namespace);
                if (TargetNamespace != null && TargetNamespace.Length != 0)
                {
                    nss.Add("tns", TargetNamespace);
                }
            }

            XmlSchemaSerializer     xser   = new XmlSchemaSerializer();
            XmlSerializerNamespaces backup = Namespaces;

            try {
                Namespaces = null;
                xser.Serialize(writer, this, nss);
            } finally {
                Namespaces = backup;
            }
            writer.Flush();
        }