Xml Powershell给了我一个“机会”;不能对空值表达式调用方法。”;但是,它起作用了——前后矛盾。发生了什么事?
TL:DR,我有三段几乎相同的PowerShell代码,结果不同 我是一个PowerShell新手,使用它来更改XML文件。在下面的文件中,我试图更改两个android:color字段和android:src字段。(顺便说一句,以下仅用于说明目的。这是我试图了解发生了什么以及如何使其工作的结果) 这是PowerShell完成其工作后的文件:Xml Powershell给了我一个“机会”;不能对空值表达式调用方法。”;但是,它起作用了——前后矛盾。发生了什么事?,xml,powershell,xamarin.forms,Xml,Powershell,Xamarin.forms,TL:DR,我有三段几乎相同的PowerShell代码,结果不同 我是一个PowerShell新手,使用它来更改XML文件。在下面的文件中,我试图更改两个android:color字段和android:src字段。(顺便说一句,以下仅用于说明目的。这是我试图了解发生了什么以及如何使其工作的结果) 这是PowerShell完成其工作后的文件: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="htt
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<foo>
<color android:color="FirstValue" />
</foo>
<item>
<color android:color="SecondValue" />
</item>
<item>
<bitmap android:src="@drawable/square150x150logo" android:tileMode="disabled" android:gravity="center" />
</item>
</layer-list>
那么,为什么第二个可以工作,但PowerShell给出了错误消息?
我怎样才能让第三个开始工作?
谢谢 根据XML,您有两个“item”XML元素。 因此,我认为您可能需要使用数组表示法来引用单个元素。查看下面的代码段是否有效
写入主机“启动测试”
$xdoc.“图层列表”.foo.color.SetAttribute(
“颜色”,
"http://schemas.android.com/apk/res/android",
“第一价值”)
如果(-not$?){
写入主机“一个失败”
}
$xdoc.“图层列表”。项目[0]。color.SetAttribute(
“颜色”,
"http://schemas.android.com/apk/res/android",
“第二价值”)
如果(-not$?){
写入主机“两个失败”
}
$xdoc.“图层列表”。项[1]。位图.SetAttribute(
“src”,
"http://schemas.android.com/apk/res/android",
“@drawable/Splash_150.png”)
如果(-not$?){
写入主机“三个失败”
}
$xdoc.Save($SplashPath)
写入主机“splashscreen.xml数据更新”
$xdoc.“图层列表”。item.color
似乎是一个数组。作为解决方法,请使用数组索引语法$xdoc.“层列表”.item.color[0]
。我还没有弄明白为什么它是一个数组,而第一个不是。像一个符咒一样工作!现在我看到了答案,当然是一系列的问题。谢谢
Write-Host "Start test"
$xdoc."layer-list".foo.color.SetAttribute(
"color",
"http://schemas.android.com/apk/res/android",
"FirstValue")
if(-not $?){
Write-Host "one failed"
}
$xdoc."layer-list".item.color.SetAttribute(
"color",
"http://schemas.android.com/apk/res/android",
"SecondValue")
if(-not $?){
Write-Host "two failed"
}
$xdoc."layer-list".item.bitmap.SetAttribute(
"src",
"http://schemas.android.com/apk/res/android",
"@drawable/Splash_150.png")
if(-not $?){
Write-Host "three failed"
}
$xdoc.Save($SplashPath)
Write-Host "splashscreen.xml data updated"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<foo>
<color android:color="FirstValue" />
</foo>
<item>
<color android:color="SecondValue" />
</item>
<item>
<bitmap android:src="@drawable/square150x150logo" android:tileMode="disabled" android:gravity="center" />
</item>
</layer-list>
1> Start test
1> FirstValue
1> You cannot call a method on a null-valued expression.
1> At C:\Users\will\Documents\XF\myApp\SetConfig.ps1:105 char:6
1> + $xdoc."layer-list".item.color.SetAttribute(
1> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1> + CategoryInfo : InvalidOperation: (:) [], RuntimeException
1> + FullyQualifiedErrorId : InvokeMethodOnNull
1>
1> two failed
1> You cannot call a method on a null-valued expression.
1> At C:\Users\will\Documents\XF\myApp\SetConfig.ps1:113 char:7
1> + $xdoc."layer-list".item.bitmap.SetAttribute(
1> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1> + CategoryInfo : InvalidOperation: (:) [], RuntimeException
1> + FullyQualifiedErrorId : InvokeMethodOnNull
1>
1> three failed
1> splashscreen.xml data updated
1> Ending Powershell