From a882a65ab1e3210bc590565284a7c7da9f57dbc1 Mon Sep 17 00:00:00 2001 From: Atsushi Eno Date: Thu, 7 Jun 2012 12:57:31 +0900 Subject: [PATCH] Use non-prefixed namespace output for element when applicable. Fixed bug #5519. --- mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs | 10 ++++++---- mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs b/mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs index eb379a1d503..3586a086a88 100644 --- a/mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs +++ b/mcs/class/System.Xml.Linq/System.Xml.Linq/XElement.cs @@ -667,13 +667,15 @@ namespace System.Xml.Linq return prefix; } - static string CreateDummyNamespace (ref int createdNS, IEnumerable atts) + static string CreateDummyNamespace (ref int createdNS, IEnumerable 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); } } diff --git a/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs b/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs index 3a89e1299e3..3a21ee24718 100644 --- a/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs +++ b/mcs/class/System.Xml.Linq/Test/System.Xml.Linq/XElementTest.cs @@ -1722,5 +1722,13 @@ namespace MonoTests.System.Xml.Linq string expected = @"".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 ("", newElement.ToString (), "#1"); + } } } -- 2.11.4.GIT