private XPathDocument loadXmlContent() { if (xmlFileName != null) { return(new XPathDocument(xmlFileName)); } System.IO.StringReader xmlContent = new System.IO.StringReader(XmlTaskHelper.JoinItems(lines)); return(new XPathDocument(xmlContent)); }
/// <summary> /// When overridden in a derived class, executes the task. /// </summary> /// <returns> /// True if the task successfully executed; otherwise, false. /// </returns> public override bool Execute() { if (!validParameters()) { return(false); } XPathDocument document = loadXmlContent(); XPathNavigator navigator = document.CreateNavigator(); XmlNamespaceManager namespaceManager = new XmlNamespaceManager(navigator.NameTable); XmlTaskHelper.LoadNamespaceDefinitionItems(namespaceManager, namespaceDefinitions); XPathExpression expression = XPathExpression.Compile(xpath, namespaceManager); //Expressions that return a node set can be used in the Select and Evaluate methods. Expressions that return a Boolean, number, or string can be used in the Evaluate method. switch (expression.ReturnType) { case XPathResultType.Boolean: case XPathResultType.Number: case XPathResultType.String: values.Add(new TaskItem(navigator.Evaluate(expression).ToString())); break; case XPathResultType.NodeSet: XPathNodeIterator nodes = navigator.Select(expression); while (nodes.MoveNext()) { values.Add(new XmlNodeTaskItem(nodes.Current, reservedMetaDataPrefix)); } break; default: throw new ArgumentException("Unable to evaluate XPath expression.", "XPath"); } return(true); }
/// <summary> /// When overridden in a derived class, executes the task. /// </summary> /// <returns> /// True if the task successfully executed; otherwise, false. /// </returns> public override bool Execute() { if (substitutionsFile == null) { substitutionsFile = contentFile; } if (mergedFile == null) { mergedFile = contentFile; } setContentPath(); setSubstitutionsPath(); setMergedPath(); if (String.IsNullOrEmpty(substitutionsRoot)) { substitutionsRoot = "/"; } if (String.IsNullOrEmpty(contentRoot)) { contentRoot = "/"; } if (contentPathUsedByTask.Equals(substitutionsPathUsedByTask, StringComparison.InvariantCultureIgnoreCase) && (contentRoot == substitutionsRoot)) { Log.LogError("The SubstitutionsRoot must be different from the ContentRoot when the ContentFile and SubstitutionsFile are the same."); return(false); } XmlDocument contentDocument = LoadContentDocument(); if (contentDocument == null) { return(false); } XmlDocument substitutionsDocument = LoadSubstitutionsDocument(); if (substitutionsDocument == null) { return(false); } namespaceManager = new XmlNamespaceManager(contentDocument.NameTable); XmlTaskHelper.LoadNamespaceDefinitionItems(namespaceManager, namespaceDefinitions); XmlNode substitutionsRootNode = substitutionsDocument.SelectSingleNode(substitutionsRoot, namespaceManager); XmlNode contentRootNode = contentDocument.SelectSingleNode(contentRoot, namespaceManager); if (substitutionsRootNode == null) { Log.LogError("Unable to locate '{0}' in {1}.", substitutionsRoot, substitutionsPathUsedByTask); return(false); } if (contentRootNode == null) { Log.LogError("Unable to locate '{0}' in {1}.", contentRoot, contentPathUsedByTask); return(false); } try { addAllChildNodes(contentDocument, contentRootNode, substitutionsRootNode); } catch (MultipleRootNodesException) { Log.LogError("Cannot create a new document root node because one already exists. Make sure to set the SubstitutionsRoot property."); return(false); } return(SaveMergedDocument(contentDocument)); }