Browse Source

2006-04-14 Atsushi Enomoto <[email protected]>

	* XmlReader.cs : ReadElementContentAsObject() incorrectly rejected
	  the argument type System.Object.

	* XmlReaderCommonTests.cs : added test for ReadElementContentAs()
	  with argument typeof(object).


svn path=/trunk/mcs/; revision=59479
Atsushi Eno 19 years ago
parent
commit
4bc7d6b705

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

@@ -1,3 +1,8 @@
+2006-04-14  Atsushi Enomoto <[email protected]>
+
+	* XmlReader.cs : ReadElementContentAsObject() incorrectly rejected
+	  the argument type System.Object.
+
 2006-04-10  Atsushi Enomoto <[email protected]>
 
 	* XmlWriter.cs : WriteNode(XPathNavigator) : directly write element

+ 14 - 3
mcs/class/System.XML/System.Xml/XmlReader.cs

@@ -878,7 +878,7 @@ namespace System.Xml
 				case XmlNodeType.CDATA:
 					break;
 				default:
-					throw new InvalidOperationException (String.Format ("Node type {0} is not supported in this operation.", NodeType));
+					throw new InvalidOperationException (String.Format ("Node type {0} is not supported in this operation.{1}", NodeType, GetLocation ()));
 				}
 			}
 
@@ -902,6 +902,13 @@ namespace System.Xml
 			throw XmlError ("Unexpected end of document.");
 		}
 
+		string GetLocation ()
+		{
+			IXmlLineInfo li = this as IXmlLineInfo;
+			return li != null && li.HasLineInfo () ?
+				String.Format (" {0} (line {1}, column {2})", BaseURI, li.LineNumber, li.LinePosition) : String.Empty;
+		}
+
 		[MonoTODO]
 		public virtual object ReadElementContentAsObject ()
 		{
@@ -922,9 +929,11 @@ namespace System.Xml
 
 		public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver)
 		{
+			bool isEmpty = IsEmptyElement;
 			ReadStartElement ();
-			object obj = ValueAs (ReadContentAsString (), type, resolver);
-			ReadEndElement ();
+			object obj = ValueAs (isEmpty ? String.Empty : ReadContentString (false), type, resolver);
+			if (!isEmpty)
+				ReadEndElement ();
 			return obj;
 		}
 
@@ -944,6 +953,8 @@ namespace System.Xml
 		private object ValueAs (string text, Type type, IXmlNamespaceResolver resolver)
 		{
 			try {
+				if (type == typeof (object))
+					return text;
 				if (type == typeof (XmlQualifiedName)) {
 					if (resolver != null)
 						return XmlQualifiedName.Parse (text, resolver);

+ 5 - 0
mcs/class/System.XML/Test/System.Xml/ChangeLog

@@ -1,3 +1,8 @@
+2006-04-14  Atsushi Enomoto <[email protected]>
+
+	* XmlReaderCommonTests.cs : added test for ReadElementContentAs()
+	  with argument typeof(object).
+
 2006-04-12  Atsushi Enomoto <[email protected]>
 
 	* XmlReaderSettingsTests.cs : 

+ 22 - 0
mcs/class/System.XML/Test/System.Xml/XmlReaderCommonTests.cs

@@ -1895,6 +1895,28 @@ namespace MonoTests.System.Xml
 			AssertEquals ("#2",
 				XmlNodeType.None, r.NodeType);
 		}
+
+		[Test]
+		public void ReadElementContentAs ()
+		{
+			// as System.Object
+
+			XmlTextReader r = new XmlTextReader (
+				"<root/>", XmlNodeType.Document, null);
+			r.Read ();
+			AssertEquals ("#1",
+				String.Empty, r.ReadElementContentAs (typeof (object), null));
+			AssertEquals ("#2",
+				XmlNodeType.None, r.NodeType);
+
+			// regardless of its value, the return value is string.
+			r = new XmlTextReader ("<root>1</root>", XmlNodeType.Document, null);
+			r.Read ();
+			AssertEquals ("#3",
+				"1", r.ReadElementContentAs (typeof (object), null));
+			AssertEquals ("#4",
+				XmlNodeType.None, r.NodeType);
+		}
 #endif
 	}
 }