コード例 #1
0
        public static object Deserialize(string xml, Type t)
        {
            if (t == null)
            {
                return(null);
            }

            XmlReaderSettings settings = null;

            if (_NameTables.ContainsKey(t))
            {
                settings = _NameTables[t];
            }

            using (TextReader textReader = new StringReader(xml))
            {
                if (settings != null)
                {
                    using (XmlExtendableReader reader = new XmlExtendableReader(textReader, settings, true))
                    {
                        return(XmlSerializerCollection.GetXmlSerializer(t).Deserialize(reader));
                    }
                }
                else
                {
                    settings = new XmlReaderSettings()
                    {
                        CheckCharacters              = false,
                        ConformanceLevel             = ConformanceLevel.Document,
                        DtdProcessing                = DtdProcessing.Ignore,
                        IgnoreComments               = true,
                        IgnoreProcessingInstructions = true,
                        IgnoreWhitespace             = true,
                        ValidationType               = ValidationType.None,
                        NameTable = null
                    };

                    using (XmlExtendableReader reader = new XmlExtendableReader(textReader, settings, true))
                    {
                        object o = XmlSerializerCollection.GetXmlSerializer(t).Deserialize(reader);

                        settings.NameTable = reader.NameTable;
                        lock (_NameTables)
                            _NameTables[t] = settings;

                        return(o);
                    }
                }
            }
        }
コード例 #2
0
        public static string Serialize(object o)
        {
            if (o == null)
            {
                return("");
            }

            XmlWriterSettings xws = new XmlWriterSettings();

            xws.OmitXmlDeclaration = true;
            xws.Indent             = true;
            xws.IndentChars        = "   ";

            using (StringWriter sw = new StringWriter())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(sw, xws))
                {
                    XmlSerializerCollection.GetXmlSerializer(o.GetType()).Serialize(xmlWriter, o);
                    return(sw.ToString());
                }
            }
        }