How to XML deserialize an object of Unknown Type?

前端 未结 5 1435
终归单人心
终归单人心 2020-12-21 11:54

I want to save my object to hard disk (like cache) with XmlSerializer. In this case, I don\'t have any problem.

However, when I want to deserialize thi

5条回答
  •  一生所求
    2020-12-21 12:09

    Assuming C#

    This is my solution: Just save the string to a text file or whatever you want to name it.

    Here is the usage:

    var xmlString = XmlHelper.Serialize(myObject);
    var myNewObject = XmlHelper.Deserialize(xmlString);
    

    Here is the Class:

    public static class XmlHelper
        {
            /// 
            /// Gets the XML from an object, used like: var test = this.Serialize(response); for troubleshooting.
            /// 
            /// The object.
            /// 
            public static string Serialize(object pObject)
            {
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(pObject.GetType());
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                serializer.Serialize(sw, pObject);
                return Beautify(sb.ToString());
            }
    
            /// 
            /// Deserializes the specified input.
            /// 
            /// 
            /// The input.
            /// 
            public static T Deserialize(string input)
            where T : class
            {
                System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
    
                using (StringReader sr = new StringReader(input))
                    return (T)ser.Deserialize(sr);
            }
    
            /// 
            /// Beautifies the specified XML stuff.
            /// 
            /// The XML stuff.
            /// 
            public static string Beautify(string xmlStuff)
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(xmlStuff);
    
                string strRetValue = null;
                System.Text.Encoding enc = System.Text.Encoding.UTF8;
                // enc = new System.Text.UTF8Encoding(false);
    
                System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings
                {
                    Encoding = enc,
                    Indent = true,
                    IndentChars = "    ",
                    NewLineChars = "\r\n",
                    NewLineHandling = System.Xml.NewLineHandling.Replace,
                    //xmlWriterSettings.OmitXmlDeclaration = true;
                    ConformanceLevel = System.Xml.ConformanceLevel.Document
                };
    
    
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(ms, xmlWriterSettings))
                    {
                        doc.Save(writer);
                        writer.Flush();
                        ms.Flush();
    
                        writer.Close();
                    } // End Using writer
    
                    ms.Position = 0;
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(ms, enc))
                    {
                        // Extract the text from the StreamReader.
                        strRetValue = sr.ReadToEnd();
    
                        sr.Close();
                    } // End Using sr
    
                    ms.Close();
                } // End Using ms
    
    
                /*
                System.Text.StringBuilder sb = new System.Text.StringBuilder(); // Always yields UTF-16, no matter the set encoding
                using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sb, settings))
                {
                    doc.Save(writer);
                    writer.Close();
                } // End Using writer
                strRetValue = sb.ToString();
                sb.Length = 0;
                sb = null;
                */
    
                xmlWriterSettings = null;
                return strRetValue;
            } // End Function Beautify
        }
    

提交回复
热议问题