1

Given this XML:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build">
  <PropertyGroup>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
  </PropertyGroup>
</Project>

I would like to change the "v.4.6.1" by "v4.7.1" using PowerShell.

Currently, I load the XML above with this:

$content = [xml](Get-Content $path.FullName)

1 Answer 1

4
$content.Project.PropertyGroup.TargetFrameworkVersion = 'v4.7.1'

This relies on PowerShell conveniently exposing an XML document's DOM by decorating the [System.Xml.XmlNode] instances that make up the object hierarchy with properties that reflect a node's XML attributes and child elements, which enables regular dot notation.

Sign up to request clarification or add additional context in comments.

2 Comments

Would this work if the .xml file were "large?" Perhaps 500 GiB? More than most machines have physical memory. Will it efficiently page?
@lit: It probably doesn't scale well. As a rule of thumb, the convenience that PowerShell offers comes at a cost: typically, performance-wise (execution speed); while the pipeline offers streaming, one-by-one processing, it can't be leveraged here; for huge files that don't fit into memory, you'll need a streaming parser such as [XmlReader](docs.microsoft.com/en-us/dotnet/api/…) - you can probably make that work in PowerShell, though I haven't tried. It sounds like an interesting topic to explore, so I encourage you to ask a new question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.