public override Stream Load(Context context) { var doc = new XmlDocument(); context.LogInfo("Loading file: {0}", FilePath); doc.Load(FilePath); if (null != UpdateXml) { foreach (var xpath in UpdateXml) { context.LogInfo("Selecting node in document, description: {0}, XPath: {1}", xpath.Description, xpath.XPath); XPathNavigator xpn = doc.CreateNavigator(); XPathNavigator node = xpn.SelectSingleNode(xpath.XPath); if (null == node) { context.LogError("XPath expression failed to find node"); throw new ApplicationException(String.Format("Node not found: {0}", xpath.Description)); } if (!string.IsNullOrEmpty(xpath.ContextKey)) { context.LogInfo("Updating XmlNode with value from context key: {0}", xpath.ContextKey); node.SetValue(context.GetValue(xpath.ContextKey)); } else { context.LogInfo("Updating XmlNode with value: {0}", xpath.Value); node.SetValue(xpath.Value); } } } MemoryStream ms = new MemoryStream(); doc.Save(ms); ms.Seek(0, SeekOrigin.Begin); return ms; }
private void ValidateXPathExpressions(XmlDocument doc, Context context) { foreach (XPathDefinition validation in _xPathValidations) { var xpathExp = validation.XPath; var expectedValue = validation.Value; if (null != validation.Description) { context.LogInfo("XPath: {0}", validation.Description); } context.LogInfo("Evaluting XPath {0} equals \"{1}\"", xpathExp, expectedValue); XPathNavigator xpn = doc.CreateNavigator(); object result = xpn.Evaluate(xpathExp); string actualValue = null; if (result.GetType().Name == "XPathSelectionIterator") { var xpi = result as XPathNodeIterator; xpi.MoveNext(); // BUGBUG! actualValue = xpi.Current.ToString(); } else { actualValue = result.ToString(); } if (!string.IsNullOrEmpty(validation.ContextKey)) { context.Add(validation.ContextKey, actualValue); } if (!string.IsNullOrEmpty(expectedValue)) { if (0 != expectedValue.CompareTo(actualValue)) { context.LogError("XPath evaluation failed. Expected:<{0}>. Actual:<{1}>.", expectedValue, actualValue); throw new ApplicationException( string.Format("XmlValidationStep failed, compare {0} != {1}, xpath query used: {2}", expectedValue, actualValue, xpathExp)); } context.LogInfo("XPath evaluation succeeded. Expected:<{0}>. Actual:<{1}>.", expectedValue, actualValue); } } }