Add mobile build support in System.Runtime.Serialization.
[mono.git] / mcs / class / referencesource / System.Runtime.Serialization / System / Runtime / Serialization / XmlSerializableServices.cs
1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //-----------------------------------------------------------------------------
4
5 namespace System.Runtime.Serialization
6 {
7     using System.Collections.Generic;
8     using System.Xml;
9     using System.Xml.Schema;
10
11     public static class XmlSerializableServices
12     {
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)
17         {
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())
23             {
24                 do
25                 {
26                     if (IsValidAttribute(xmlReader))
27                     {
28                         XmlNode node = doc.ReadNode(xmlReader);
29                         if (node == null)
30                             throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.UnexpectedEndOfFile)));
31                         nodeList.Add(node);
32                     }
33                 } while (xmlReader.MoveToNextAttribute());
34             }
35             xmlReader.MoveToElement();
36             if (!xmlReader.IsEmptyElement)
37             {
38                 int startDepth = xmlReader.Depth;
39                 xmlReader.Read();
40                 while (xmlReader.Depth > startDepth && xmlReader.NodeType != XmlNodeType.EndElement)
41                 {
42                     XmlNode node = doc.ReadNode(xmlReader);
43                     if (node == null)
44                         throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.UnexpectedEndOfFile)));
45                     nodeList.Add(node);
46                 }
47             }
48             return nodeList.ToArray();
49         }
50
51         private static bool IsValidAttribute(XmlReader xmlReader)
52         {
53             return xmlReader.NamespaceURI != Globals.SerializationNamespace &&
54                                    xmlReader.NamespaceURI != Globals.SchemaInstanceNamespace &&
55                                    xmlReader.Prefix != "xmlns" &&
56                                    xmlReader.LocalName != "xmlns";
57         }
58
59         internal static string WriteNodesMethodName = "WriteNodes";
60         public static void WriteNodes(XmlWriter xmlWriter, XmlNode[] nodes)
61         {
62             if (xmlWriter == null)
63                 throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("xmlWriter");
64             if (nodes != null)
65                 for (int i = 0; i < nodes.Length; i++)
66                     if (nodes[i] != null)
67                         nodes[i].WriteTo(xmlWriter);
68         }
69
70 #if !MOBILE
71         internal static string AddDefaultSchemaMethodName = "AddDefaultSchema";
72         public static void AddDefaultSchema(XmlSchemaSet schemas, XmlQualifiedName typeQName)
73         {
74             if (schemas == null)
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);
79         }
80 #endif
81     }
82 }