Selaa lähdekoodia

2005-03-29 Lluis Sanchez Gual <[email protected]>

	* XmlReflectionImporter.cs: Added support for subclasses of XmlNode.
	This fixes bug #73901 and should fix #70384.
	* XmlSerializationReader.cs: When reading an object element, return
	an Object instance if the element has no children. This fixes bug #73974.
	* XmlSerializationWriter.cs: Support writing XmlNode[] as a primitive
	type (it is written as an element with those nodes as children). 


svn path=/trunk/mcs/; revision=42328
Lluis Sanchez 21 vuotta sitten
vanhempi
sitoutus
a3fc0d1cd0

+ 9 - 0
mcs/class/System.XML/System.Xml.Serialization/ChangeLog

@@ -1,3 +1,12 @@
+2005-03-29  Lluis Sanchez Gual  <[email protected]>
+
+	* XmlReflectionImporter.cs: Added support for subclasses of XmlNode.
+	This fixes bug #73901 and should fix #70384.
+	* XmlSerializationReader.cs: When reading an object element, return
+	an Object instance if the element has no children. This fixes bug #73974.
+	* XmlSerializationWriter.cs: Support writing XmlNode[] as a primitive
+	type (it is written as an element with those nodes as children). 
+
 2005-03-08  Lluis Sanchez Gual <[email protected]>
 
 	* XmlSchemaImporter.cs: Support importing schemas that define

+ 11 - 24
mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs

@@ -492,31 +492,18 @@ namespace System.Xml.Serialization {
 			XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
 			if (map != null) return map;
 
-			// Registers the maps for XmlNode and XmlElement
-
-			XmlTypeMapping nodeMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlNode)), root, null, defaultNamespace);
-			helper.RegisterClrType (nodeMap, typeof(XmlNode), nodeMap.XmlTypeNamespace);
-
-			XmlTypeMapping elemMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlElement)), root, null, defaultNamespace);
-			helper.RegisterClrType (elemMap, typeof(XmlElement), elemMap.XmlTypeNamespace);
-
-			XmlTypeMapping textMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlText)), root, null, defaultNamespace);
-			helper.RegisterClrType (textMap, typeof(XmlText), textMap.XmlTypeNamespace);
-
-			XmlTypeMapping docMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlDocument)), root, null, defaultNamespace);
-			helper.RegisterClrType (docMap, typeof(XmlDocument), textMap.XmlTypeNamespace);
-
-			XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
-			obmap.DerivedTypes.Add (nodeMap);
-			obmap.DerivedTypes.Add (elemMap);
-			obmap.DerivedTypes.Add (textMap);
-			obmap.DerivedTypes.Add (docMap);
-			nodeMap.DerivedTypes.Add (elemMap);
-			nodeMap.DerivedTypes.Add (textMap);
-			nodeMap.DerivedTypes.Add (docMap);
+			map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
+			helper.RegisterClrType (map, type, map.XmlTypeNamespace);
+			
+			if (type.BaseType != null)
+			{
+				XmlTypeMapping bmap = ImportTypeMapping (type.BaseType, root, defaultNamespace);
+				if (type.BaseType != typeof (object))
+					map.BaseMap = bmap;
+				
+				RegisterDerivedMap (bmap, map);
+			}
 
-			map = helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
-			if (map == null) throw new InvalidOperationException ("Objects of type '" + type + "' can't be serialized");
 			return map;
 		}
 

+ 5 - 1
mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReader.cs

@@ -706,9 +706,13 @@ namespace System.Xml.Serialization
 			{
 				// Put everything into a node array
 				XmlNode node = Document.ReadNode (reader);
+
+				if (node.ChildNodes.Count == 0 && node.Attributes.Count == 0)
+					return new Object ();
+
 				XmlElement elem = node as XmlElement;
 				
-				if (elem == null) 
+				if (elem == null)
 					return new XmlNode[] {node};
 				else {
 					XmlNode[] nodes = new XmlNode[elem.Attributes.Count + elem.ChildNodes.Count];

+ 17 - 11
mcs/class/System.XML/System.Xml.Serialization/XmlSerializationWriter.cs

@@ -795,19 +795,25 @@ namespace System.Xml.Serialization
 			name = XmlCustomFormatter.FromXmlName (name);
 			Writer.WriteStartElement (name, ns);
 
-			if (o is XmlQualifiedName)
-				value = FromXmlQualifiedName ((XmlQualifiedName) o);
-			else
-				value = XmlCustomFormatter.ToXmlString (td, o);
-
-			if (xsiType)
-			{
-				if (td.SchemaType != SchemaTypes.Primitive)
-					throw new InvalidOperationException (string.Format (unexpectedTypeError, o.GetType().FullName));
-				WriteXsiType (td.XmlType, XmlSchema.Namespace);
+			if (o is XmlNode[]) {
+				foreach (XmlNode node in (XmlNode[])o)
+					node.WriteTo (Writer);
 			}
+			else {
+				if (o is XmlQualifiedName)
+					value = FromXmlQualifiedName ((XmlQualifiedName) o);
+				else
+					value = XmlCustomFormatter.ToXmlString (td, o);
 
-			WriteValue (value);
+				if (xsiType)
+				{
+					if (td.SchemaType != SchemaTypes.Primitive)
+						throw new InvalidOperationException (string.Format (unexpectedTypeError, o.GetType().FullName));
+					WriteXsiType (td.XmlType, XmlSchema.Namespace);
+				}
+
+				WriteValue (value);
+			}
 			Writer.WriteEndElement ();
 		}