diff --git a/Source/MSBuild.Community.Tasks/MSBuild.Community.Tasks.csproj b/Source/MSBuild.Community.Tasks/MSBuild.Community.Tasks.csproj index bb8fb3da..f4a16a13 100644 --- a/Source/MSBuild.Community.Tasks/MSBuild.Community.Tasks.csproj +++ b/Source/MSBuild.Community.Tasks/MSBuild.Community.Tasks.csproj @@ -59,6 +59,7 @@ + diff --git a/Source/MSBuild.Community.Tasks/XmlUpdate.cs b/Source/MSBuild.Community.Tasks/XmlUpdate.cs index 4dd5a425..b24a7834 100644 --- a/Source/MSBuild.Community.Tasks/XmlUpdate.cs +++ b/Source/MSBuild.Community.Tasks/XmlUpdate.cs @@ -31,9 +31,11 @@ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. using System.Collections.Generic; using System.Text; using System.Xml; +using System.Linq; using Microsoft.Build.Utilities; using Microsoft.Build.Framework; using System.Xml.XPath; +using System.Xml.Linq; @@ -152,35 +154,50 @@ public override bool Execute() try { Log.LogMessage(Properties.Resources.XmlUpdateDocument, _xmlFileName); - - XmlDocument document = new XmlDocument(); - document.Load(_xmlFileName); - - XPathNavigator navigator = document.CreateNavigator(); - XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable); + + XDocument xdoc = XDocument.Load(_xmlFileName); + XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable()); if (!string.IsNullOrEmpty(_prefix) && !string.IsNullOrEmpty(_namespace)) { manager.AddNamespace(_prefix, _namespace); } - - XPathExpression expression = XPathExpression.Compile(_xpath, manager); - XPathNodeIterator nodes = navigator.Select(expression); - - Log.LogMessage(Properties.Resources.XmlUpdateNodes, nodes.Count); - while (nodes.MoveNext()) - if (_delete) - nodes.Current.DeleteSelf(); - else - nodes.Current.SetValue(_value ?? string.Empty); + + var items = xdoc.XPathEvaluate(_xpath, manager) as IEnumerable; + + Log.LogMessage(Properties.Resources.XmlUpdateNodes, items.Count()); - using (XmlTextWriter writer = new XmlTextWriter(_xmlFileName, Encoding.UTF8)) + foreach (var item in items.ToArray()) { - writer.Formatting = Formatting.Indented; - document.Save(writer); - writer.Close(); + var attr = item as XAttribute; + if (attr != null) + { + if (_delete) + { + attr.Remove(); + } + else + { + attr.SetValue(_value); + } + } + + var ele = item as XElement; + if (ele != null) + { + if (_delete) + { + ele.Remove(); + } + else + { + ele.SetValue(_value); + } + } } + + xdoc.Save(_xmlFileName); } catch (Exception ex) {