XmlElementElement.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System;
  7. using System.Configuration;
  8. using System.Runtime;
  9. using System.Security;
  10. using System.Xml;
  11. public sealed partial class XmlElementElement : ConfigurationElement
  12. {
  13. public XmlElementElement()
  14. {
  15. }
  16. public XmlElementElement(XmlElement element) : this()
  17. {
  18. this.XmlElement = element;
  19. }
  20. public void Copy(XmlElementElement source)
  21. {
  22. if (this.IsReadOnly())
  23. {
  24. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigReadOnly)));
  25. }
  26. if (null == source)
  27. {
  28. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("source");
  29. }
  30. if (null != source.XmlElement)
  31. {
  32. this.XmlElement = (XmlElement)source.XmlElement.Clone();
  33. }
  34. }
  35. [Fx.Tag.SecurityNote(Critical = "Uses the critical helper SetIsPresent.",
  36. Safe = "Controls how/when SetIsPresent is used, not arbitrarily callable from PT (method is protected and class is sealed).")]
  37. [SecuritySafeCritical]
  38. protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
  39. {
  40. SetIsPresent();
  41. DeserializeElementCore(reader);
  42. }
  43. void DeserializeElementCore(XmlReader reader)
  44. {
  45. XmlDocument doc = new XmlDocument();
  46. this.XmlElement = (XmlElement)doc.ReadNode(reader);
  47. }
  48. internal void ResetInternal(XmlElementElement element)
  49. {
  50. this.Reset(element);
  51. }
  52. [Fx.Tag.SecurityNote(Critical = "Calls ConfigurationHelpers.SetIsPresent which elevates in order to set a property.",
  53. Safe = "Only passes 'this', does not let caller influence parameter.")]
  54. [SecurityCritical]
  55. void SetIsPresent()
  56. {
  57. ConfigurationHelpers.SetIsPresent(this);
  58. }
  59. protected override bool SerializeToXmlElement(XmlWriter writer, String elementName)
  60. {
  61. bool dataToWrite = this.XmlElement != null;
  62. if (dataToWrite && writer != null)
  63. {
  64. if (!String.Equals(elementName, ConfigurationStrings.XmlElement, StringComparison.Ordinal))
  65. {
  66. writer.WriteStartElement(elementName);
  67. }
  68. using (XmlNodeReader reader = new XmlNodeReader(this.XmlElement))
  69. {
  70. writer.WriteNode(reader, false);
  71. }
  72. if (!String.Equals(elementName, ConfigurationStrings.XmlElement, StringComparison.Ordinal))
  73. {
  74. writer.WriteEndElement();
  75. }
  76. }
  77. return dataToWrite;
  78. }
  79. protected override void PostDeserialize()
  80. {
  81. this.Validate();
  82. base.PostDeserialize();
  83. }
  84. void Validate()
  85. {
  86. if (this.XmlElement == null)
  87. {
  88. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigXmlElementMustBeSet),
  89. this.ElementInformation.Source,
  90. this.ElementInformation.LineNumber));
  91. }
  92. }
  93. [ConfigurationProperty(ConfigurationStrings.XmlElement, DefaultValue = null, Options = ConfigurationPropertyOptions.IsKey)]
  94. public XmlElement XmlElement
  95. {
  96. get { return (XmlElement)base[ConfigurationStrings.XmlElement]; }
  97. set { base[ConfigurationStrings.XmlElement] = value; }
  98. }
  99. }
  100. }