XmlSerializableServices.cs 3.8 KB

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