* XmlSerializationWriter.cs: When writing the root element, use a prefix
authorLluis Sanchez <lluis@novell.com>
Tue, 17 Feb 2004 11:15:50 +0000 (11:15 -0000)
committerLluis Sanchez <lluis@novell.com>
Tue, 17 Feb 2004 11:15:50 +0000 (11:15 -0000)
  if the namespace of the element is defined in the list of namespaces
  provided to the XmlSerializer. This fixes bug #54427.

svn path=/trunk/mcs/; revision=23163

mcs/class/System.XML/System.Xml.Serialization/ChangeLog
mcs/class/System.XML/System.Xml.Serialization/XmlSerializationWriter.cs

index d7611b0e4533bdcdc444a9fbf6fad104368e7cf2..bcb811328b817389cc7050e63c1187bb4833fe56 100755 (executable)
@@ -1,3 +1,9 @@
+2004-02-17  Lluis Sanchez Gual  <lluis@ximian.com>
+
+       * XmlSerializationWriter.cs: When writing the root element, use a prefix
+         if the namespace of the element is defined in the list of namespaces
+         provided to the XmlSerializer. This fixes bug #54427.
+
 2004-02-16  Lluis Sanchez Gual  <lluis@ximian.com>
 
        * MapCodeGenerator.cs: Modified some methods to make them easier to reuse.
index d16178a9ae917cadd34ec7bb3f09bd3ed407e639..b83f61c787e12cbe59f91c9caf09b9026c5987e8 100644 (file)
@@ -675,14 +675,29 @@ namespace System.Xml.Serialization {
                        }
                        
                        WriteState oldState = Writer.WriteState;
-
+                       
                        // Elements with schema namespace are always written prefixed
                        if (ns == XmlSchema.Namespace) writePrefixed = true;
 
-                       if (writePrefixed && ns != string.Empty) {
+                       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);
-                               string prefix = Writer.LookupPrefix (ns);
-                               if (prefix == null) {
+                               
+                               if (prefix == null)
+                                       prefix = Writer.LookupPrefix (ns);
+                               if (prefix == null || prefix.Length == 0) {
                                        if (ns == XmlSchema.Namespace) prefix = "xsd";
                                        else prefix = "q" + (++qnameCount);
                                }
@@ -695,11 +710,12 @@ namespace System.Xml.Serialization {
                                if (namespaces != null) {
                                        foreach (XmlQualifiedName qn in namespaces)
                                        {
+                                               string currentPrefix = Writer.LookupPrefix (qn.Namespace);
                                                if (qn.Namespace == XmlSchema.Namespace || qn.Namespace == XmlSchema.InstanceNamespace) {
-                                                       if (Writer.LookupPrefix (qn.Namespace) == qn.Name) continue;
+                                                       if (currentPrefix == qn.Name) continue;
                                                }
                                                else 
-                                                       if (Writer.LookupPrefix (qn.Namespace) != null) continue;
+                                                       if (currentPrefix != null && currentPrefix.Length != 0) continue;
                                                
                                                WriteAttribute ("xmlns",qn.Name,xmlNamespace,qn.Namespace);
                                        }