protected virtual SettingsXml LoadSettings(string settingsFilename) { //ensure the file is existing if (!File.Exists(settingsFilename)) { throw new ArgumentException(string.Format("The file '{0}' has been referenced for settings by the configuration file but this file hasn't been not found!", settingsFilename)); } //Create an empty XmlRoot. //This is needed because the class settingsXml is not decorated with an attribute "XmlRoot". XmlRootAttribute xmlRoot = new XmlRootAttribute { ElementName = "settings", Namespace = "http://NBi/TestSuite", IsNullable = true }; SettingsXml settings = null; // Create the XmlReader object. using (var xmlReader = BuildXmlReaderForSettings(settingsFilename, false)) { var overrides = new ReadOnlyAttributes(); overrides.Build(); // Create an instance of the XmlSerializer specifying type. var serializer = new XmlSerializer(typeof(SettingsXml), overrides, null, xmlRoot, string.Empty); // Use the Deserialize method to restore the object's state. settings = (SettingsXml)serializer.Deserialize(xmlReader); } return(settings); }
public void Deserialize_Equal_WithoutCDATA() { var xml = "<PredicationXml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><equal><|>|&</equal></PredicationXml>"; var manager = new XmlManager(); var overrides = new ReadOnlyAttributes(); overrides.Build(); var objectData = manager.XmlDeserializeTo <PredicationXml>(xml, overrides); Assert.That(objectData, Is.TypeOf <PredicationXml>()); Assert.That(objectData, Is.Not.Null); Assert.That(objectData.Predicate, Is.TypeOf <EqualXml>()); Assert.That(objectData.Predicate, Is.Not.Null); Assert.That(objectData.Predicate.Value, Is.EqualTo("<|>|&")); }
public void Deserialize_MatchesRegex_WithCDATA() { var xml = "<PredicationXml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><matches-regex><![CDATA[<|>|&]]></matches-regex></PredicationXml>"; var manager = new XmlManager(); var overrides = new ReadOnlyAttributes(); overrides.Build(); var objectData = manager.XmlDeserializeTo <PredicationXml>(xml, overrides); Assert.That(objectData, Is.TypeOf <PredicationXml>()); Assert.That(objectData, Is.Not.Null); Assert.That(objectData.Predicate, Is.TypeOf <MatchesRegexXml>()); Assert.That(objectData.Predicate, Is.Not.Null); Assert.That(objectData.Predicate.Value, Is.EqualTo("<|>|&")); }
protected object XmlDeserializeFromString(string objectData, Type type) { var overrides = new ReadOnlyAttributes(); overrides.Build(); var serializer = new XmlSerializer(type, overrides); object result; using (TextReader reader = new StringReader(objectData)) { result = serializer.Deserialize(reader); } return(result); }
public void Deserialize_Equal_WithCDATA() { var xml = "<SinglePredicationXml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><equal><![CDATA[<|>|&]]></equal></SinglePredicationXml>"; var manager = new XmlManager(); var overrides = new ReadOnlyAttributes(); overrides.Build(); var objectData = manager.XmlDeserializeTo <SinglePredicationXml>(xml, overrides); Assert.That(objectData, Is.TypeOf <SinglePredicationXml>()); Assert.That(objectData, Is.Not.Null); Assert.That(objectData.Predicate, Is.TypeOf <EqualXml>()); var predicate = objectData.Predicate as EqualXml; Assert.That(predicate, Is.Not.Null); Assert.That(predicate.Reference, Is.EqualTo("<|>|&")); }
public void Deserialize_MatchesRegex_WithoutCDATA() { var xml = "<SinglePredicationXml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><matches-regex><|>|&</matches-regex></SinglePredicationXml>"; var manager = new XmlManager(); var overrides = new ReadOnlyAttributes(); overrides.Build(); var objectData = manager.XmlDeserializeTo <SinglePredicationXml>(xml, overrides); Assert.That(objectData, Is.TypeOf <SinglePredicationXml>()); Assert.That(objectData, Is.Not.Null); Assert.That(objectData.Predicate, Is.TypeOf <MatchesRegexXml>()); var predicate = objectData.Predicate as MatchesRegexXml; Assert.That(predicate, Is.Not.Null); Assert.That(predicate.Reference, Is.EqualTo("<|>|&")); }
protected object XmlDeserializeFromString(string objectData, Type type) { if (!cacheDeserializer.ContainsKey(type)) { var overrides = new ReadOnlyAttributes(); overrides.Build(); var builtDeserializer = new XmlSerializer(type, overrides); cacheDeserializer.Add(type, builtDeserializer); } var serializer = cacheDeserializer[type]; object result; using (TextReader reader = new StringReader(objectData)) { result = serializer.Deserialize(reader); } return(result); }
protected virtual void Read(XmlReader reader) { //Add the attributes that should only be used during read phase //These attributes are kept for compatibility with previous versions //They should never been used during write process var attrs = new ReadOnlyAttributes(); attrs.Build(); // Create an instance of the XmlSerializer specifying type and read-attributes. try { validationExceptions.Clear(); XmlSerializer serializer = new XmlSerializer(typeof(TestSuiteXml), attrs); // Use the Deserialize method to restore the object's state. TestSuite = (TestSuiteXml)serializer.Deserialize(reader); } catch (InvalidOperationException ex) { if (ex.InnerException is XmlException) { XmlSchemaException xmlSchemaException; if (ex.InnerException.Message.Contains("For security reasons DTD is prohibited")) { xmlSchemaException = new XmlSchemaException("DTD is prohibited. To activate it, set the flag allow-dtd-processing to true in the config file associated to this test-suite"); } else { var regex = new Regex(@"Line (\d+), position (\d+).$"); var match = regex.Match(ex.InnerException.Message); if (match.Success) { int line = 0; Int32.TryParse(match.Groups[1].Value, out line); int position = 0; Int32.TryParse(match.Groups[2].Value, out position); xmlSchemaException = new XmlSchemaException(ex.InnerException.Message, ex, line, position); } else { xmlSchemaException = new XmlSchemaException(ex.InnerException.Message); } } Console.WriteLine(xmlSchemaException.Message); validationExceptions.Add(xmlSchemaException); } else { ParseCascadingInvalidOperationException(ex.InnerException as InvalidOperationException); } } if (validationExceptions.Count > 0) { var message = "The test suite is not valid. Check with the XSD."; message += string.Format(" {0} error{1} {2} been found during the validation of the test-suite:\r\n" , validationExceptions.Count , validationExceptions.Count > 1 ? "s" : string.Empty , validationExceptions.Count > 1 ? "have" : "has"); foreach (var error in validationExceptions) { message += string.Format("\tAt line {0}: {1}\r\n", error.LineNumber, error.Message); } throw new ArgumentException(message); } }