| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Configuration; |
| | | 6 | | using System.Xml; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Configuration |
| | | 9 | | { |
| | | 10 | | public sealed class XmlElementElement : ConfigurationElement |
| | | 11 | | { |
| | 0 | 12 | | public XmlElementElement() |
| | | 13 | | { |
| | 0 | 14 | | } |
| | | 15 | | |
| | 0 | 16 | | public XmlElementElement(XmlElement element) : this() |
| | | 17 | | { |
| | 0 | 18 | | XmlElement = element; |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public void Copy(XmlElementElement source) |
| | | 22 | | { |
| | 0 | 23 | | if (IsReadOnly()) |
| | | 24 | | { |
| | 0 | 25 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.Format(SR. |
| | | 26 | | } |
| | 0 | 27 | | if (null == source) |
| | | 28 | | { |
| | 0 | 29 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(source)); |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | if (null != source.XmlElement) |
| | | 33 | | { |
| | 0 | 34 | | XmlElement = (XmlElement)source.XmlElement.Clone(); |
| | | 35 | | } |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey) |
| | | 39 | | { |
| | 0 | 40 | | DeserializeElementCore(reader); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | private void DeserializeElementCore(XmlReader reader) |
| | | 44 | | { |
| | 0 | 45 | | XmlDocument doc = new XmlDocument(); |
| | 0 | 46 | | XmlElement = (XmlElement)doc.ReadNode(reader); |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | internal void ResetInternal(XmlElementElement element) |
| | | 50 | | { |
| | 0 | 51 | | Reset(element); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | protected override bool SerializeToXmlElement(XmlWriter writer, string elementName) |
| | | 55 | | { |
| | 0 | 56 | | bool dataToWrite = XmlElement != null; |
| | 0 | 57 | | if (dataToWrite && writer != null) |
| | | 58 | | { |
| | 0 | 59 | | if (!string.Equals(elementName, ConfigurationStrings.XmlElement, StringComparison.Ordinal)) |
| | | 60 | | { |
| | 0 | 61 | | writer.WriteStartElement(elementName); |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | using (XmlNodeReader reader = new XmlNodeReader(XmlElement)) |
| | | 65 | | { |
| | 0 | 66 | | writer.WriteNode(reader, false); |
| | 0 | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | if (!string.Equals(elementName, ConfigurationStrings.XmlElement, StringComparison.Ordinal)) |
| | | 70 | | { |
| | 0 | 71 | | writer.WriteEndElement(); |
| | | 72 | | } |
| | | 73 | | } |
| | 0 | 74 | | return dataToWrite; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | protected override void PostDeserialize() |
| | | 78 | | { |
| | 0 | 79 | | Validate(); |
| | 0 | 80 | | base.PostDeserialize(); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | private void Validate() |
| | | 84 | | { |
| | 0 | 85 | | if (XmlElement == null) |
| | | 86 | | { |
| | 0 | 87 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.Format(SR. |
| | 0 | 88 | | ElementInformation.Source, |
| | 0 | 89 | | ElementInformation.LineNumber)); |
| | | 90 | | } |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | [ConfigurationProperty(ConfigurationStrings.XmlElement, DefaultValue = null, Options = ConfigurationPropertyOpti |
| | | 94 | | public XmlElement XmlElement |
| | | 95 | | { |
| | 0 | 96 | | get { return (XmlElement)base[ConfigurationStrings.XmlElement]; } |
| | 0 | 97 | | set { base[ConfigurationStrings.XmlElement] = value; } |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |