我第一次在PowerShell中使用XML文件。我有一个简单的脚本失败了。我需要使用web-request获取XML内容,然后将其保存到一个文件夹中以供以后处理。
以下是代码:
$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")
当我运行这个程序时,出现了以下错误:
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
我做错了什么?
System.Xml.XmlElement
没有名为 'save' 或 'Save' 的方法 - 问题在于强制转换,因此更改大小写不会有所帮助。 - arco444System.Xml.XmlDocument
确实有一个Save()方法,但他们正在覆盖$ipgeo
。 - CodeCaster.Response
,它是一个XmlElement
,所以如果在下一行代码中用其他东西覆盖它,实例化什么并不重要。 - arco444