/// <summary> /// /// </summary> /// <param name="log"></param> /// <param name="testId"></param> /// <param name="pathA"></param> /// <param name="pathB"></param> /// <param name="errorCounter"></param> public static void CompareXml(ITestLog log, string testId, string pathA, string pathB, Counter errorCounter) { log.LogInfo($" CompareXml: pathA={pathA}", testId); log.LogInfo($" CompareXml: pathB={pathB}", testId); try { // load original into xml doc var docA = new XmlDocument(); using (var fs = new FileStream(pathA, FileMode.Open, FileAccess.Read)) using (var sr = new StreamReader(fs, Encoding.UTF8)) { docA.Load(sr); } // load emitted stream into xml doc var docB = new XmlDocument(); using (var stream = new FileStream(pathB, FileMode.Open, FileAccess.Read)) { docB.Load(stream); } // compare XmlCompare.CompareXmlDocs(docA, docB); } catch (Exception excp) { errorCounter.Inc(); log.LogException(excp, testId, false); } }
/// <summary> /// /// </summary> /// <param name="log"></param> /// <param name="testId"></param> /// <param name="xmlDocPath"></param> /// <param name="schemaSet"></param> /// <param name="errorCounter"></param> /// <param name="warningCounter"></param> public static void ValidateXml( ITestLog log, string testId, string xmlDocPath, XmlSchemaSet schemaSet, Counter errorCounter, Counter warningCounter) { log.LogInfo($" ValidateXml: xmlDocPath={xmlDocPath}", testId); //log.LogInfo(String.Format(" ValidateXml: outputPath={0}", outputPath ?? "(null)"), testId); using (var stream = new FileStream(xmlDocPath, FileMode.Open, FileAccess.Read)) { var sampleDoc = new XmlDocument(); sampleDoc.Schemas.Add(schemaSet); sampleDoc.Load(stream); sampleDoc.Validate((o, e) => { if (e.Severity == XmlSeverityType.Error) { errorCounter.Inc(); } else { warningCounter.Inc(); } log.LogError($"Validation {e.Severity}: {e.Message}", testId, false); log.LogException(e.Exception, testId, false); }); } }
/// <summary> /// /// </summary> /// <param name="log"></param> /// <param name="testId"></param> /// <param name="sourcePath"></param> /// <param name="outputPath"></param> /// <param name="errorCounter"></param> public static void FormatXml(ITestLog log, string testId, string sourcePath, string outputPath, Counter errorCounter) { log.LogInfo($" FormatXml: sourcePath={sourcePath}", testId); log.LogInfo($" FormatXml: outputPath={outputPath}", testId); try { var sampleDoc = new XmlDocument(); using (var xr = new XmlTextReader(sourcePath)) { sampleDoc.Load(xr); } var xws = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 }; using (XmlWriter xw = XmlWriter.Create(outputPath, xws)) { sampleDoc.Save(xw); } } catch (Exception excp) { errorCounter.Inc(); log.LogException(excp, testId, false); } }
/// <summary> /// /// </summary> /// <param name="log"></param> /// <param name="testId"></param> /// <param name="transformer"></param> /// <param name="sourcePath"></param> /// <param name="targetPath"></param> /// <param name="errorCounter"></param> public static void TransformXml(ITestLog log, string testId, IXmlTransformer transformer, string sourcePath, string targetPath, Counter errorCounter) { log.LogInfo($" TransformXml: sourcePath={sourcePath}", testId); log.LogInfo($" TransformXml: targetPath={targetPath}", testId); try { transformer.Transform(sourcePath, targetPath); } catch (Exception excp) { errorCounter.Inc(); log.LogException(excp, testId, false); } }
/// <summary> /// /// </summary> /// <param name="log"></param> /// <param name="testId"></param> /// <param name="typeDetector"></param> /// <param name="sourcePath"></param> /// <param name="errorCounter"></param> /// <returns></returns> public static object DeserialiseXml(ITestLog log, string testId, Func <string, Type> typeDetector, string sourcePath, Counter errorCounter) { log.LogInfo($" DeserialiseXml: sourcePath={sourcePath}", testId); try { Type autoDetectedType = typeDetector(sourcePath); var xs = new XmlSerializer(autoDetectedType); using (var xr = new XmlTextReader(sourcePath)) { return(xs.Deserialize(xr)); } } catch (Exception excp) { errorCounter.Inc(); log.LogException(excp, testId, false); return(null); } }
/// <summary> /// /// </summary> /// <param name="log"></param> /// <param name="testId"></param> /// <param name="fpml"></param> /// <param name="outputPath"></param> /// <param name="errorCounter"></param> public static void SerialiseXml(ITestLog log, string testId, object fpml, string outputPath, Counter errorCounter) { log.LogInfo($" SerialiseXml: outputPath={outputPath}", testId); try { var xs = new XmlSerializer(fpml.GetType()); var xws = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 }; using (var xw = XmlWriter.Create(outputPath, xws)) //using (var fs = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) //using (var xw = new XmlTextWriter(fs, Encoding.UTF8)) { xs.Serialize(xw, fpml); } } catch (Exception excp) { errorCounter.Inc(); log.LogException(excp, testId, false); } }