Powershell: write XML to file

Dou*_*oug 4 xml powershell

I'm working with XML files for the first time in PowerShell. I have a simple script that fails. I need to get XML content using web-request and then save it to a folder for later processing.

Here is the code:

$IP = 8.8.8.8
$ipgeo = new-object System.Xml.XmlDocument
$ipgeo = ([xml](Invoke-WebRequest "http://freegeoip.net/xml/$IP").Content).Response
$ipgeo.save("c:\ipgeo\IPXML\$IP.xml")
Run Code Online (Sandbox Code Playgroud)

When I run this, I get the following error:

Method invocation failed because [System.Xml.XmlElement] does not contain a method named 'save'. At line:3 char:1
+ $ipgeo.save("c:\ipgeo\IPXML\$IP.xml")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo: InvalidOperation: (save:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Run Code Online (Sandbox Code Playgroud)

What am I doing wrong?

Mat*_*sen 5

你可以通过引用OuterXml你想要的根节点的属性来保存 Xml 文档的一个子集:

# instead of $ipgeo.Save("c:\ipgeo\IPXML\$IP.xml")
$ipgeo.OuterXml |Out-File "c:\ipgeo\IPXML\$IP.xml"
Run Code Online (Sandbox Code Playgroud)