public XDocument SerializeToXDocument(object obj) { XDocument xdoc = new XDocument(); string defaultNamespace = DataContractSerializer_Helpers.GetDefaultNamespace(_type.Namespace, _useXmlSerializerFormat); //todo: USEMETHODTOGETTYPE: make a method that returns the actual type of object and replace all USEMETHODTOGETTYPE with a call to this method. (also find other places where we could use it) Type objectType = obj.GetType(); if (obj is char) //special case because JSIL thinks a variable of type object containing a char contains a string. { objectType = typeof(char); } TypeInformation typeInformation = null; if (objectType != _type) { // Get the type information (namespace, etc.) by reading the DataContractAttribute and similar attributes, if present: typeInformation = DataContractSerializer_Helpers.GetTypeInformationByReadingAttributes(_type, defaultNamespace, _useXmlSerializerFormat); } // Add the root: List <XObject> xnodesForRoot = DataContractSerializer_Serialization.SerializeToXObjects(obj, _type, _knownTypes, _useXmlSerializerFormat, isRoot: true, isContainedInsideEnumerable: false, parentTypeInformation: typeInformation, nodeDefaultNamespaceIfAny: null); xdoc.Add(xnodesForRoot.First()); return(xdoc); }
public string SerializeToString(object obj, bool indentXml = false, bool omitXmlDeclaration = false) { XDocument xdoc = new XDocument(); string defaultNamespace = DataContractSerializer_Helpers.GetDefaultNamespace(_type.Namespace, _useXmlSerializerFormat); //todo: USEMETHODTOGETTYPE: make a method that returns the actual type of object and replace all USEMETHODTOGETTYPE with a call to this method. (also find other places where we could use it) Type objectType = obj.GetType(); if (obj is char) //special case because JSIL thinks a variable of type object containing a char contains a string. { objectType = typeof(char); } TypeInformation typeInformation = null; if (objectType != _type) { // Get the type information (namespace, etc.) by reading the DataContractAttribute and similar attributes, if present: typeInformation = DataContractSerializer_Helpers.GetTypeInformationByReadingAttributes(_type, defaultNamespace, _useXmlSerializerFormat); } // Add the root: List <XObject> xnodesForRoot = DataContractSerializer_Serialization.SerializeToXObjects(obj, _type, _knownTypes, _useXmlSerializerFormat, isRoot: true, isContainedInsideEnumerable: false, parentTypeInformation: typeInformation, nodeDefaultNamespaceIfAny: null); xdoc.Add(xnodesForRoot.First()); #if CSHTML5NETSTANDARD string xml = xdoc.ToString(indentXml ? SaveOptions.None : SaveOptions.DisableFormatting); #else string xml = xdoc.ToString(indentXml); #endif // Add the header: if (!omitXmlDeclaration) { xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>" + Environment.NewLine + xml; } return(xml); }