Surgical XML editing with Powershell


I’m working with csproj files using Powershell to perform large-scale editing of project references. So far I’ve managed to edit the Include attributes on ProjectReferences using the following lines:

This works, and replaces the Include attribute on the appropriate ProjectReferences as I expect. However, there are plenty of additional “harmless” changes made, such as formatting all tags on their own line, e.g.




Is there any way to perform an edit such as this that doesn’t have these side effects?

edit: for clarity to anyone who finds this post for other reasons, Select-MsBuildXml is just a wrapper function I wrote around Select-Xml that pre-loads the namespace parameter with the msbuild namespace, and expands the node property afterwards.


I did a lot of VS project manipulation a few years back. Seems like creating the XmlDocument and using Load directly (versus using Get-Content and casting to XML) worked better for me:

Update – Try setting the PreserveWhitespace property to true before loading the XML document as shown above.


Surgical XML editing with Powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply