How does XML deserialize an object of Unknown type?

I want to save my object to a hard drive (e.g. cache) using XmlSerializer . In this case, I have no problem.

However, when I want to deserialize this XML object for an object, I get an error. Is there anyway to deserialize the XML to an unknown object or to the object that I created?

+1
source share
5 answers

In .Net there is no way to deserialize an unknown object.

To successfully serialize / deserialize an XML object, the class must have a default constructor. The best way is to show the exact error message. Can you do it?

+1
source

Why not serialize your class type first (System.Type class is Serializable)?

Then you can check which type has been serialized and create the appropriate instance.

0
source

This is my solution: just save the line in a text file or whatever you want it to be called.

Here is the usage:

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

Here is the class:

 public static class XmlHelper { /// <summary> /// Gets the XML from an object, used like: var test = this.Serialize(response); for troubleshooting. /// </summary> /// <param name="pObject">The object.</param> /// <returns></returns> 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()); } /// <summary> /// Deserializes the specified input. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="input">The input.</param> /// <returns></returns> public static T Deserialize<T>(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); } /// <summary> /// Beautifies the specified XML stuff. /// </summary> /// <param name="xmlStuff">The XML stuff.</param> /// <returns></returns> 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 } 
0
source

Another, more efficient (either DOM or SAX) data binding approach is in this article :

-2
source

you can use SerializationHelper.DeSerializeNow as described in my post: http://borismod.blogspot.com/2008/07/nunit-serialization-test.html

 internal class SerializationHelper { private static readonly string DefaultFilePath = "test.dat"; internal static void SerializeNow(object c) { SerializeNow(c, DefaultFilePath); } internal static void SerializeNow(object c, string filepath) { FileInfo f = new FileInfo(filepath); using (Stream s = f.Open(FileMode.Create)) { BinaryFormatter b = new BinaryFormatter(); b.Serialize(s, c); } } internal static object DeSerializeNow() { return DeSerializeNow(DefaultFilePath); } internal static object DeSerializeNow(string filepath) { FileInfo f = new FileInfo(filepath); using (Stream s = f.Open(FileMode.Open)) { BinaryFormatter b = new BinaryFormatter(); return b.Deserialize(s); } } } 
-2
source

Source: https://habr.com/ru/post/1439322/


All Articles