Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access #text property of XMLAttribute in powershell

I have an xml document formated like this:

<root>
<obj>
   <indexlist>
      <index name="NUMD" value="val1" />
      <index name="DATE" value="val2" />
   </indexlist>
</obj>
</root>

now I'd like to change the value attribute of the index element where name is set to "DATE". I get the attribute like this:

$attr = $xml.selectnodes("//obj/indexlist/index[@name='DATE']/@value")

I can view the value by typing this:

$attr.'#text'

but I can't change it:

$attr.'#text' = 'foo'
The property '#text' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:1
+ $n.'#text' = 'foo'
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

how do I change the value of an XMLAttribute?


I'd also like to stick with XPath returning the attribute directly if that's possible because the end-user of that script will define the elements and attributes to change in a config file using XPath.
While using XPath for the attributes as well the user can simply provide the attribute to change and the future-value with just two arguments: the XPath and the value.

like image 395
wullxz Avatar asked Mar 25 '15 15:03

wullxz


People also ask

What is the full meaning of access?

1 : the right or ability to approach, enter, or use Only a few have access to the secret information. 2 : a way or means of approaching access to the sea. access. verb. accessed; accessing.

What is access example?

An example of access is permission to enter a secure area. The ability, opportunity, permission, or right to approach, communicate, enter, pass to and from, or view without interference or obstruction.

What is the adverb form of access?

Word family (noun) access accessibility ≠ inaccessibility (adjective) accessible ≠ inaccessible (verb) access (adverb) accessibly ≠ inaccessibly.

What is the synonym of access?

admission, admittance, entry, entrée, ingress, right of entry, permission to enter, the opportunity to enter.


1 Answers

Besides #text, you can also access XmlAttribute's value via Value property :

$attr = $xml.SelectSingleNode("//obj/indexlist/index[@name='DATE']/@value")

#print old value
$attr.Value

#update attribute value 
$attr.Value = "new value"

#print new value
$attr.Value

Note that Value in $attr.Value is property name of XmlAttribute. It doesn't affected by the fact that the attribute in your XML named value.

like image 122
har07 Avatar answered Sep 21 '22 14:09

har07