1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.Runtime.Serialization
7 using System.Collections.Generic;
9 using System.Xml.Schema;
11 public static class XmlSerializableServices
13 [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview - Static fields are marked SecurityCritical or readonly to prevent"
14 + " data from being modified or leaked to other components in appdomain.")]
15 internal static readonly string ReadNodesMethodName = "ReadNodes";
16 public static XmlNode[] ReadNodes(XmlReader xmlReader)
18 if (xmlReader == null)
19 throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("xmlReader");
20 XmlDocument doc = new XmlDocument();
21 List<XmlNode> nodeList = new List<XmlNode>();
22 if (xmlReader.MoveToFirstAttribute())
26 if (IsValidAttribute(xmlReader))
28 XmlNode node = doc.ReadNode(xmlReader);
30 throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.UnexpectedEndOfFile)));
33 } while (xmlReader.MoveToNextAttribute());
35 xmlReader.MoveToElement();
36 if (!xmlReader.IsEmptyElement)
38 int startDepth = xmlReader.Depth;
40 while (xmlReader.Depth > startDepth && xmlReader.NodeType != XmlNodeType.EndElement)
42 XmlNode node = doc.ReadNode(xmlReader);
44 throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.UnexpectedEndOfFile)));
48 return nodeList.ToArray();
51 private static bool IsValidAttribute(XmlReader xmlReader)
53 return xmlReader.NamespaceURI != Globals.SerializationNamespace &&
54 xmlReader.NamespaceURI != Globals.SchemaInstanceNamespace &&
55 xmlReader.Prefix != "xmlns" &&
56 xmlReader.LocalName != "xmlns";
59 internal static string WriteNodesMethodName = "WriteNodes";
60 public static void WriteNodes(XmlWriter xmlWriter, XmlNode[] nodes)
62 if (xmlWriter == null)
63 throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("xmlWriter");
65 for (int i = 0; i < nodes.Length; i++)
67 nodes[i].WriteTo(xmlWriter);
71 internal static string AddDefaultSchemaMethodName = "AddDefaultSchema";
72 public static void AddDefaultSchema(XmlSchemaSet schemas, XmlQualifiedName typeQName)
75 throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("schemas");
76 if (typeQName == null)
77 throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("typeQName");
78 SchemaExporter.AddDefaultXmlType(schemas, typeQName.Name, typeQName.Namespace);