2009-01-08 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Thu, 8 Jan 2009 02:39:23 +0000 (02:39 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Thu, 8 Jan 2009 02:39:23 +0000 (02:39 -0000)
* XmlReader.cs : ReadContentAsString() does not reject non-elements.
  ReadElementContentAsString() rejects non-content nodes.

* XmlReaderCommonTests.cs : added test for ReadContentAsString()
  and ReadElementContentAsString() against certain node types.

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

mcs/class/System.XML/System.Xml/ChangeLog
mcs/class/System.XML/System.Xml/XmlReader.cs
mcs/class/System.XML/Test/System.Xml/ChangeLog
mcs/class/System.XML/Test/System.Xml/XmlReaderCommonTests.cs

index 0e208fe332afa58434b47cb3bdaa163c8bb674ec..56cf54b3c86afbe4fdbee15fa66f9f793568d029 100644 (file)
@@ -1,3 +1,8 @@
+2009-01-08  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * XmlReader.cs : ReadContentAsString() does not reject non-elements.
+         ReadElementContentAsString() rejects non-content nodes.
+
 2008-12-19  Atsushi Enomoto  <atsushi@ximian.com>
 
        * XmlNamespaceManager.cs : namespace lookup failed for non-atomic
index 2d5aa54c09ba7f371633e47b31057525a0cf7d93..40c6e05d9b878c910ebaf8ffa3c1c341f94b9569 100644 (file)
@@ -901,8 +901,10 @@ namespace System.Xml
                                case XmlNodeType.Whitespace:
                                case XmlNodeType.CDATA:
                                        break;
-                               default:
+                               case XmlNodeType.Element:
                                        throw new InvalidOperationException (String.Format ("Node type {0} is not supported in this operation.{1}", NodeType, GetLocation ()));
+                               default:
+                                       return String.Empty;
                                }
                        }
 
@@ -1076,6 +1078,9 @@ namespace System.Xml
                public virtual string ReadElementContentAsString ()
                {
                        bool isEmpty = IsEmptyElement;
+                       // unlike ReadStartElement() it rejects non-content nodes (this check is done before MoveToContent())
+                       if (NodeType != XmlNodeType.Element)
+                               throw new InvalidOperationException (String.Format ("'{0}' is an element node.", NodeType));
                        ReadStartElement ();
                        if (isEmpty)
                                return String.Empty;
@@ -1150,6 +1155,9 @@ namespace System.Xml
                public virtual string ReadElementContentAsString (string localName, string namespaceURI)
                {
                        bool isEmpty = IsEmptyElement;
+                       // unlike ReadStartElement() it rejects non-content nodes (this check is done before MoveToContent())
+                       if (NodeType != XmlNodeType.Element)
+                               throw new InvalidOperationException (String.Format ("'{0}' is an element node.", NodeType));
                        ReadStartElement (localName, namespaceURI);
                        if (isEmpty)
                                return String.Empty;
index 5ae95eb4df162b2999bab46e3a288c4d93699db7..5f1f8d6c0143a3b44cec4eb3ca587759ed5245e8 100644 (file)
@@ -1,3 +1,8 @@
+2009-01-08  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * XmlReaderCommonTests.cs : added test for ReadContentAsString()
+         and ReadElementContentAsString() against certain node types.
+
 2008-12-09  Atsushi Enomoto  <atsushi@ximian.com>
 
        * XmlValidatingReaderTests.cs : added test for validating mixed
index 95f30cf1a51e74b4deb0c6cec37cd37b43db1ea2..de28a8235b4d28bb77ae39a4c59e8345e498231a 100644 (file)
@@ -1951,6 +1951,34 @@ namespace MonoTests.System.Xml
                        xr.ReadContentAsString ();\r
                }\r
 \r
+               [Test]\r
+               public void ReadContentStringOnEndElement ()\r
+               {\r
+                       XmlReader xr = XmlReader.Create (new StringReader ("<a>test</a>"));\r
+                       xr.Read ();\r
+                       xr.Read ();\r
+                       xr.Read ();\r
+                       AssertEquals (String.Empty, xr.ReadContentAsString ()); // does not fail, unlike at Element!\r
+               }\r
+\r
+               [Test]\r
+               public void ReadContentStringOnPI ()\r
+               {\r
+                       XmlReader xr = XmlReader.Create (new StringReader ("<?pi ?><a>test</a>"));\r
+                       xr.Read ();\r
+                       AssertEquals (String.Empty, xr.ReadContentAsString ());\r
+               }\r
+\r
+               [Test]\r
+               [ExpectedException (typeof (InvalidOperationException))] // unlike ReadContentAsString()\r
+               public void ReadElementContentStringOnPI ()\r
+               {\r
+                       XmlReader xr = XmlReader.Create (new StringReader ("<?pi ?><a>test</a>"));\r
+                       xr.Read ();\r
+                       AssertEquals (XmlNodeType.ProcessingInstruction, xr.NodeType);\r
+                       xr.ReadElementContentAsString ();\r
+               }\r
+\r
                [Test]\r
                [ExpectedException (typeof (XmlException))]\r
                public void ReadElementContentStringMixedContent ()\r