Skip to content

Commit 1375350

Browse files
committed
Merge pull request #138 from ManniManfred/master
Use XDocument instead of XmlDocument
2 parents 3f1f03c + 21f620d commit 1375350

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

Source/MSBuild.Community.Tasks/MSBuild.Community.Tasks.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<Reference Include="System.Management" />
6060
<Reference Include="System.ServiceProcess" />
6161
<Reference Include="System.Xml" />
62+
<Reference Include="System.Xml.Linq" />
6263
</ItemGroup>
6364
<ItemGroup>
6465
<Compile Include="..\GlobalAssemblyInfo.cs">

Source/MSBuild.Community.Tasks/XmlUpdate.cs

+37-20
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
3131
using System.Collections.Generic;
3232
using System.Text;
3333
using System.Xml;
34+
using System.Linq;
3435
using Microsoft.Build.Utilities;
3536
using Microsoft.Build.Framework;
3637
using System.Xml.XPath;
38+
using System.Xml.Linq;
3739

3840

3941

@@ -152,35 +154,50 @@ public override bool Execute()
152154
try
153155
{
154156
Log.LogMessage(Properties.Resources.XmlUpdateDocument, _xmlFileName);
155-
156-
XmlDocument document = new XmlDocument();
157-
document.Load(_xmlFileName);
158-
159-
XPathNavigator navigator = document.CreateNavigator();
160-
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
157+
158+
XDocument xdoc = XDocument.Load(_xmlFileName);
159+
XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
161160

162161
if (!string.IsNullOrEmpty(_prefix) && !string.IsNullOrEmpty(_namespace))
163162
{
164163
manager.AddNamespace(_prefix, _namespace);
165164
}
166-
167-
XPathExpression expression = XPathExpression.Compile(_xpath, manager);
168-
XPathNodeIterator nodes = navigator.Select(expression);
169-
170-
Log.LogMessage(Properties.Resources.XmlUpdateNodes, nodes.Count);
171165

172-
while (nodes.MoveNext())
173-
if (_delete)
174-
nodes.Current.DeleteSelf();
175-
else
176-
nodes.Current.SetValue(_value ?? string.Empty);
166+
167+
var items = xdoc.XPathEvaluate(_xpath, manager) as IEnumerable<object>;
168+
169+
Log.LogMessage(Properties.Resources.XmlUpdateNodes, items.Count());
177170

178-
using (XmlTextWriter writer = new XmlTextWriter(_xmlFileName, Encoding.UTF8))
171+
foreach (var item in items.ToArray())
179172
{
180-
writer.Formatting = Formatting.Indented;
181-
document.Save(writer);
182-
writer.Close();
173+
var attr = item as XAttribute;
174+
if (attr != null)
175+
{
176+
if (_delete)
177+
{
178+
attr.Remove();
179+
}
180+
else
181+
{
182+
attr.SetValue(_value);
183+
}
184+
}
185+
186+
var ele = item as XElement;
187+
if (ele != null)
188+
{
189+
if (_delete)
190+
{
191+
ele.Remove();
192+
}
193+
else
194+
{
195+
ele.SetValue(_value);
196+
}
197+
}
183198
}
199+
200+
xdoc.Save(_xmlFileName);
184201
}
185202
catch (Exception ex)
186203
{

0 commit comments

Comments
 (0)