Use non-prefixed namespace output for element when applicable. Fixed bug #5519.
authorAtsushi Eno <atsushieno@veritas-vos-liberabit.com>
Thu, 7 Jun 2012 03:57:31 +0000 (12:57 +0900)
committerAtsushi Eno <atsushieno@veritas-vos-liberabit.com>
Thu, 7 Jun 2012 03:57:31 +0000 (12:57 +0900)
mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs
mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs

index eb379a1d503234cbbc63531f91046b99c673d29f..3586a086a8822c363c9fde812d85f630ba46a178 100644 (file)
@@ -667,13 +667,15 @@ namespace System.Xml.Linq
                        return prefix;
                }
                
-               static string CreateDummyNamespace (ref int createdNS, IEnumerable<XAttribute> atts)
+               static string CreateDummyNamespace (ref int createdNS, IEnumerable<XAttribute> atts, bool isAttr)
                {
+                       if (!isAttr && atts.All (a => a.Name.LocalName != "xmlns" || a.Name.NamespaceName == XNamespace.Xmlns.NamespaceName))
+                               return String.Empty;
                        string p = null;
                        do {
                                p = "p" + (++createdNS);
                                // check conflict
-                               if (atts.All (a => a.Name.LocalName != p))
+                               if (atts.All (a => a.Name.LocalName != p || a.Name.NamespaceName == XNamespace.Xmlns.NamespaceName))
                                        break;
                        } while (true);
                        return p;
@@ -686,7 +688,7 @@ namespace System.Xml.Linq
                        string prefix = LookupPrefix (name.NamespaceName, w);
                        int createdNS = 0;
                        if (prefix == null)
-                               prefix = CreateDummyNamespace (ref createdNS, Attributes ());
+                               prefix = CreateDummyNamespace (ref createdNS, Attributes (), false);
 
                        w.WriteStartElement (prefix, name.LocalName, name.Namespace.NamespaceName);
 
@@ -699,7 +701,7 @@ namespace System.Xml.Linq
                                } else {
                                        string apfix = LookupPrefix (a.Name.NamespaceName, w);
                                        if (apfix == null)
-                                               apfix = CreateDummyNamespace (ref createdNS, Attributes ());
+                                               apfix = CreateDummyNamespace (ref createdNS, Attributes (), true);
                                        w.WriteAttributeString (apfix, a.Name.LocalName, a.Name.Namespace.NamespaceName, a.Value);
                                }
                        }
index 3a89e1299e3bf7982c06bc3c4363d17f93f0d075..3a21ee2471893a4356ff6455c4e2156a71640f4f 100644 (file)
@@ -1722,5 +1722,13 @@ namespace MonoTests.System.Xml.Linq
                        string expected = @"<xsi:eventData xsi1:type='xsi:CallSubscriptionEvent' xmlns:xsi1='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsi='http://relevo.se/xsi' />".Replace ('\'', '"');
                        Assert.AreEqual (expected, e.Nodes ().First ().ToString (), "#1");
                }
+               
+               [Test] // bug #5519
+               public void DoUseEmptyNamespacePrefixWhenApplicable ()
+               {
+                       XNamespace ns = "http://jabber.org/protocol/geoloc";
+                       XElement newElement = new XElement(ns + "geoloc");
+                       Assert.AreEqual ("<geoloc xmlns=\"http://jabber.org/protocol/geoloc\" />", newElement.ToString (), "#1");
+               }
        }
 }