Exemplo n.º 1
0
 /// <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);
     }
 }
Exemplo n.º 2
0
 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);
     }
 }