c# - Serialise to XML and include the type of the serialised object -
In the previous question about
, I need to sort some mistake information for a XmlDocument I need to sort back to an object on an XmlDocument client from an asmx-style webservice call.
If you know this type then it is straightforward, but I realized that I want a flexible approach, where to type the code in the XmlDocument also encode has been done. I am currently manually adding a XmlNode to XmlDocument , the type of name that is, is computed like this:
type type = Fault.GetType (); String assembly name = type. Assembly. FULName; // Version and Culture Information Close assembly name = assembly name. Shuttle (0, assembly name index (",")). Trim (); String type name = type.full name + "," + assemblyName;
Then on the client I get this type of name back to XmlDocument , and the type object is that the gets passed in the XmlSerialiser < / Code> as follows:
object mistake; XMLNode Galitnode = E.Titrate First Shield; XMLNode mistakeTypeNnode = Galentonode. // Type of type of fault type is the first node string type xml within name name = faultTypeNode.InnerXml; Type faultType = Type.GetType (typeName); // The sequential data for the fault is the second node (var stream = new stringreader (Foltonode.opplex)) {var serialiser = new XmlSerializer (faultType); ObjectThatWasSerialized = serialiser.Deserialize (current); } Return (cast type) mistake; So, this is a brute force approach, and if so, it is a more elegant solution that somehow it automatically involves the typename of the serial type automatically from anywhere else on the recording. I was rather thinking?
I had encountered such a problem and I came up with the same solution. As far as I am concerned, XML is the only way to keep the type together with values in the serialization.
I think you are cutting the assembly edition as I did but I want to mention, that you are feeling like a signature in the form of troubles with the generic type:
System.Nullable`1 [[System.Int, mscorlib, version = 2.0. 0.0, culture = neutral, publicKeyToken = b77a5c561934e089]], mscorlib, version = 2.0.0.0, culture = neutral, publicKeyToken = b77a5c561934e089
So I make a function to cut only the assembly version (S), which seems to get rid of much trouble verbing:
Private Static String CutOutVersionNumbers (string fullTypeName) {string shortTypeName = fullTypeName; Var Version Index = ShortTipName.indexoff ("version"); While (version index! = -1) {int commaIndex = shortTypeName.IndexOf (",", versionIndex); ShortTypeName = ShortTypeName. Remove (version index, commindex - version index + 1); Versionindex = shortcutname Indochoff ("version"); } Return shortTypeName; }
Comments
Post a Comment