コード例 #1
0
        public static void Deserialize(IXmlSerializable source, XmlReader reader)
        {
            const string method = "Deserialize";

            if (reader == null)
            {
                throw new Exceptions.NullParameterException(typeof(XmlSerializer), method, "reader");
            }

            // IXmlSerializable.ReadOuterXml() should read the entire element on which the reader is position,
            // including the EndElement node, which some implementations don't do. Check for this common problem.

            if (source != null)
            {
#if DEBUG
                Debug.Assert(reader.NodeType == XmlNodeType.None || reader.NodeType == XmlNodeType.Element,
                             string.Format("The reader node is {0} '{1}' before calling ReadOuterXml() on '{2}'.",
                                           reader.NodeType, reader.LocalName, source.GetType().FullName),
                             "Before ReadOuterXml() is called the reader should be positioned at the start of the element"
                             + " to be read by that method.");
                int depth = reader.Depth;
#endif

                source.ReadOuterXml(reader);

#if DEBUG
                Debug.Assert(depth == 0 || reader.Depth == depth - 1, string.Format("The reader node is {0} '{1}'"
                                                                                    + " after calling ReadOuterXml() on '{2}'.", reader.NodeType, reader.LocalName, source.GetType().FullName),
                             "After ReadOuterXml() is called the reader should be positioned immediately AFTER the element"
                             + " read by that method (NOT on the EndElement node of the element that was read).");
#endif
            }
        }