@@ -31,9 +31,11 @@ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31
31
using System . Collections . Generic ;
32
32
using System . Text ;
33
33
using System . Xml ;
34
+ using System . Linq ;
34
35
using Microsoft . Build . Utilities ;
35
36
using Microsoft . Build . Framework ;
36
37
using System . Xml . XPath ;
38
+ using System . Xml . Linq ;
37
39
38
40
39
41
@@ -152,35 +154,50 @@ public override bool Execute()
152
154
try
153
155
{
154
156
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 ( ) ) ;
161
160
162
161
if ( ! string . IsNullOrEmpty ( _prefix ) && ! string . IsNullOrEmpty ( _namespace ) )
163
162
{
164
163
manager . AddNamespace ( _prefix , _namespace ) ;
165
164
}
166
-
167
- XPathExpression expression = XPathExpression . Compile ( _xpath , manager ) ;
168
- XPathNodeIterator nodes = navigator . Select ( expression ) ;
169
-
170
- Log . LogMessage ( Properties . Resources . XmlUpdateNodes , nodes . Count ) ;
171
165
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 ( ) ) ;
177
170
178
- using ( XmlTextWriter writer = new XmlTextWriter ( _xmlFileName , Encoding . UTF8 ) )
171
+ foreach ( var item in items . ToArray ( ) )
179
172
{
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
+ }
183
198
}
199
+
200
+ xdoc . Save ( _xmlFileName ) ;
184
201
}
185
202
catch ( Exception ex )
186
203
{
0 commit comments