/// <summary> /// Deserialize XML string into a .net Object /// </summary> /// <typeparam name="T">Type of object to be deserialized into</typeparam> /// <param name="xml">string, containing the XML data</param> /// <param name="schema">string, containing the name of the XML schema</param> /// <param name="schemaURI">string, containing the URI of the XML schema</param> /// <returns>object of Type T with data from the XML string</returns> private static T DeserializeXML <T>(string xml, string schema, string schemaURI, bool validation) { GC.Collect(3, GCCollectionMode.Forced, true); try { using (StringReader sReader = new StringReader(xml)) { // Create temporary copy of XML Reader Settings to use if it needs to be modified XmlReaderSettings xmlReaderSettings = TestController.GetCachedXMLReaderSettings(schema, schemaURI).Clone(); if (!validation) { // remove ValidationEventHandler from temporary copy to skip validation xmlReaderSettings.ValidationEventHandler -= xmlRSettings_ValidationEventHandler; xmlReaderSettings.ValidationType = ValidationType.None; } using (XmlReader xmlReader = XmlReader.Create(sReader, xmlReaderSettings)) { XmlSerializer xmlSerializer = TestController.GetCachedXMLSerializer(typeof(T), schema); return((T)(xmlSerializer.Deserialize(xmlReader))); } } } catch (Exception ex) { throw new Exception("Failed to serialize XML: " + xml, ex); } finally { // Clear xml = null; GC.Collect(3, GCCollectionMode.Forced, true); } }
private static bool ValidateXML(Type t, string inputXML) { GC.Collect(3, GCCollectionMode.Forced, true); try { using (StringReader sReader = new StringReader(inputXML)) { using (XmlReader xmlReader = XmlReader.Create(sReader, TestController.GetCachedXMLReaderSettings(Properties.Resources.SECURE_XML_Schema, (System.IO.Directory.GetCurrentDirectory() + "\\XML\\SECURE.xsd")))) { return((TestController.GetCachedXMLSerializer(t, Properties.Resources.SECURE_XML_Schema).Deserialize(xmlReader)) != null); } } } finally { // Clear GC.Collect(3, GCCollectionMode.Forced, true); } }
/// <summary> /// Serialize the XSD generated SECURE code to XML in string format. /// </summary> /// <param name="o">The System.Object to serialize.</param> /// <param name="validate">bool, true to validate data after serialization</param> /// <returns>a UTF-8 encoded XML string</returns> public static string SerializeXML(object o, bool validate) { try { using (Utf8StringWriter stringWriter = new Utf8StringWriter()) { TestController.GetCachedXMLSerializer(o.GetType(), Properties.Resources.SECURE_XML_Schema).Serialize(stringWriter, o); if (validate) { TestController.ValidateXML(o.GetType(), stringWriter.ToString()); } return(stringWriter.ToString()); } } finally { GC.Collect(); } }