问题
Doc Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Identity PUBLIC "point.dtd" "point.dtd"[]>
<Identity created="1525465321820" name="Onboarding - GUI - External">
<Attributes>
<Map>
<entry key="displayName" value="Onboarding - GUI " />
<entry key="firstname" value="Z Orphaned ID" />
</Attributes>
</Identity>
I can assign values to displayName and firstname no problem like so:
[string] $displayvalue = $xmldata.Identity.Attributes.Map.entry[0].value
[string] $firstname = $xmldata.Identity.Attributes.Map.entry[1].value
and than save.
$myFile = 'C:\somefile.xml'
$xmldata.Save($myFile)
BUT when I try to do the same for name :
[string] $xmldata.Identity.Name = "TEST"
I got an error :
The property 'Name' cannot be found on this object. Verify that the property exists and can be set.
Thanks
回答1:
You are not trying the same thing for the name
attribute as you do for the displayname
and firstname
attributes.
With the latter two, you are READING the values from the XML in string variables. With the name
attribute, you are trying to SET it to a new value.
To do that, you first need to select the element that holds the attribute to update. Once you have that, you can use the SetAttribute()
method of the XmlElement object(s).
[xml]$xmldata = @"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Identity PUBLIC "point.dtd" "point.dtd"[]>
<Identity created="1525465321820" name="Onboarding - GUI - External">
<Attributes>
<Map>
<entry key="displayName" value="Onboarding - GUI " />
<entry key="firstname" value="Z Orphaned ID" />
</Map>
</Attributes>
</Identity>
"@
# select the node(s) to update, and update the name attribute
$xmldata.Identity | Where-Object { $_.name -like 'Onboarding*' } | ForEach-Object {
$_.SetAttribute("name", "TEST")
}
# Now save the updated XM to file:
$xmldata.Save('C:\somefile.xml')
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Identity PUBLIC "point.dtd" "point.dtd"[]>
<Identity created="1525465321820" name="TEST">
<Attributes>
<Map>
<entry key="displayName" value="Onboarding - GUI " />
<entry key="firstname" value="Z Orphaned ID" />
</Map>
</Attributes>
</Identity>
回答2:
XMLs are case sensitive and since in
<Identity created="1525465321820" name="Onboarding - GUI - External">
"name" is lowercase, you need to use
$xmldata.Identity.name
As oppose to
$xmldata.Identity.Name
and to edit the value use
$xmldata.Identity.name = "TEST"
TESTING
PS C:\Users\Neko> [xml]$xmldata = @'
>> <Identity created="1525465321820" name="Onboarding - GUI - External">
>> <Attributes>
>> <Map>
>> <entry key="displayName" value="Onboarding - GUI " />
>> <entry key="firstname" value="Z Orphaned ID" />
>> </Map>
>> </Attributes>
>> </Identity>
>> '@
PS C:\Users\Neko> $xmldata.Identity.name
Onboarding - GUI - External
PS C:\Users\Neko> $xmldata.Identity.name = "TEST"
PS C:\Users\Neko> $xmldata.Identity.name
TEST
If you want to import an xml file and do this do the following:
- Set a variable as the content of the xml file but skip the first 2 lines and set another as the first 2 lines
[xml]$var = Get-Content .\test.xml | select-object -skip 2
$var2 = Get-Content .\test.xml | select-object -first 2
- Use
$var
the ways demonstrated above
$var.Identity.name
Output:
Onboarding - GUI - External
Input:
$var.Identity.name = "TEST"
$var.Identity.name
Output:
TEST
- Then add the header back to the variable
[xml]$final = $var2 + $var.outerxml
- Finally, save the variable back to the XML file
$final.Save("filepath")
Something about
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Identity PUBLIC "point.dtd" "point.dtd"[]>
Powershell seems to not like, therefore if you skip them it works fine...
Tested in Powershell 5.1
Slightly changed your XML file since PS gave me errors when I used your exact XML since there was no closing </map>
来源:https://stackoverflow.com/questions/62202753/trying-to-update-xml-document-with-powershell-but-having-issue-updating-root-nod