Prechádzať zdrojové kódy

* XmlTextWriter.cs: when adding an attribute with a namespace, a prefix must be automaticaly
generated, and a namespace declaration must be added.
* XmlWriter.cs: the namespace for the prefix xmlns must be http://www.w3.org/2000/xmlns/

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

Lluis Sanchez 22 rokov pred
rodič
commit
a3e2c2fc98

+ 6 - 0
mcs/class/System.XML/System.Xml/ChangeLog

@@ -1,3 +1,9 @@
+2003-06-10  Lluis Sanchez Gual <[email protected]>
+
+	* XmlTextWriter.cs: when adding an attribute with a namespace, a prefix must be automaticaly
+	  generated, and a namespace declaration must be added. 
+	* XmlWriter.cs: the namespace for the prefix xmlns must be http://www.w3.org/2000/xmlns/
+
 2003-06-10  Zoltan Varga  <[email protected]>
 
 	* XmlAttribute.cs: Accept a null prefix or namespaceURI.

+ 23 - 1
mcs/class/System.XML/System.Xml/XmlTextWriter.cs

@@ -48,6 +48,7 @@ namespace System.Xml
 		bool openElementNsAdded;
 		bool hasRoot = false;
 		Hashtable writtenAttributes = new Hashtable ();
+		ArrayList newAttributeNamespaces = new ArrayList ();
 		bool checkMultipleAttributes = false;
 
 		#endregion
@@ -221,6 +222,15 @@ namespace System.Xml
 						w.Write(formatXmlns);
 				}
 			}
+
+			for (int n=0; n<newAttributeNamespaces.Count; n++)
+			{
+				string ans = (string)newAttributeNamespaces[n];
+				string aprefix = namespaceManager.LookupPrefix (ans);
+				string formatXmlns = String.Format (" xmlns:{0}={1}{2}{1}", aprefix, quoteChar, ans);
+				w.Write(formatXmlns);
+			}
+			newAttributeNamespaces.Clear ();
 		}
 
 		private void CheckState ()
@@ -273,6 +283,7 @@ namespace System.Xml
 			attributeWrittenForElement = false;
 			checkMultipleAttributes = false;
 			writtenAttributes.Clear ();
+			newAttributeNamespaces.Clear ();
 		}
 
 		public override void Flush ()
@@ -518,6 +529,9 @@ namespace System.Xml
 			if ((prefix == "xmlns") && (localName.ToLower ().StartsWith ("xml")))
 				throw new ArgumentException ("Prefixes beginning with \"xml\" (regardless of whether the characters are uppercase, lowercase, or some combination thereof) are reserved for use by XML: " + prefix + ":" + localName);
 
+			if ((prefix == "xmlns") && (ns != "http://www.w3.org/2000/xmlns/"))
+				throw new ArgumentException ("The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'");
+
 			CheckState ();
 
 			if (ws == WriteState.Content)
@@ -532,10 +546,17 @@ namespace System.Xml
 			string formatPrefix = "";
 			string formatSpace = "";
 
-			if (ns != String.Empty) 
+			if (ns != String.Empty && prefix != "xmlns") 
 			{
 				string existingPrefix = namespaceManager.LookupPrefix (ns);
 
+				if (existingPrefix == null) 
+				{
+					newAttributeNamespaces.Add (ns);
+					prefix = "d" + indentLevel + "p" + newAttributeNamespaces.Count;
+					namespaceManager.AddNamespace (prefix, ns);
+				}
+
 				if (prefix == String.Empty && ns != "http://www.w3.org/2000/xmlns/")
 					prefix = (existingPrefix == null) ?
 						String.Empty : existingPrefix;
@@ -628,6 +649,7 @@ namespace System.Xml
 			CheckState ();
 			CloseStartElement ();
 			writtenAttributes.Clear ();
+			newAttributeNamespaces.Clear ();
 			checkMultipleAttributes = true;
 
 			if (prefix == null)

+ 5 - 6
mcs/class/System.XML/System.Xml/XmlWriter.cs

@@ -80,9 +80,8 @@ namespace System.Xml
 		public void WriteAttributeString (string prefix, string localName, string ns, string value)
 		{
 			if ((prefix == "xmlns") || (prefix == "" && localName == "xmlns"))
-			  {
-				ns = value;
-				
+			{
+				if (ns == null)  ns = "http://www.w3.org/2000/xmlns/";
 				if (prefix == "xmlns" && namespaceManager.HasNamespace (localName))
 				  	return;
 			}
@@ -95,11 +94,11 @@ namespace System.Xml
 			{
 				if (prefix == "xmlns")
 				{
-					if (ns == string.Empty) throw new ArgumentException ("Cannot use a prefix with an empty namespace");
-					namespaceManager.AddNamespace (localName, ns);
+					if (value == string.Empty) throw new ArgumentException ("Cannot use a prefix with an empty namespace");
+					namespaceManager.AddNamespace (localName, value);
 				}
 				else
-					namespaceManager.AddNamespace ("", ns);
+					namespaceManager.AddNamespace ("", value);
 			}
 			
 		}