![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Modify XML file Is there a way to modify an XML file using powershell? I poked around in the help and on google, but didn't find anything. I try to replace the contents of a single tag. The tag is unique and formatting is consistent so I could also use a simple text search-and-replace. Thanks! ph |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Modify XML file you do SOMETHING like this (just out of my head so sorry if there are mistakes) $a = [xml]get-content myfile.xml $a.rootnode.someothernode.othernodeyouwantochange = 'new data' $a > newfile.xml it probably won't run but get you started Peter Hauptmann wrote: > Is there a way to modify an XML file using powershell? I poked around > in the help and on google, but didn't find anything. > > I try to replace the contents of a single tag. The tag is unique and > formatting is consistent so I could also use a simple text > search-and-replace. > > Thanks! > ph |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Modify XML file Is there something I have to load / register for the [xml] stuff? get-contents (according to | get-member) returns only a string, and I can't find anything about xml using help thanks! klumsy@xtra.co.nz wrote: > you do SOMETHING like this (just out of my head so sorry if there are > mistakes) > > $a = [xml]get-content myfile.xml > $a.rootnode.someothernode.othernodeyouwantochange = 'new data' > $a > newfile.xml > > it probably won't run but get you started > > > Peter Hauptmann wrote: > > Is there a way to modify an XML file using powershell? I poked around > > in the help and on google, but didn't find anything. > > > > I try to replace the contents of a single tag. The tag is unique and > > formatting is consistent so I could also use a simple text > > search-and-replace. > > > > Thanks! > > ph |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Modify XML file peterchen ha scritto: > Is there something I have to load / register for the [xml] stuff? > > get-contents (according to | get-member) returns only a string, and I > can't find anything about xml using help $a = [xml]get-content myfile.xml Should be: $a = [xml] (get-content myfile.xml) Ciao! Claudio |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Modify XML file Hi, here are some more examples and a little explanation: PS>$var = [sometype] somevalue works like the cast operator in other languages. So you can do things like this: PS> [int] "5" 5 PS> [string] 5 5 PS> [double] "3.14159265358979" 3.14159265358979 PS> [int] 3.14159265358979 3 PS> [wmiclass] "Win32_DiskDrive" Win32_DiskDrive PS> $hwxml = [xml] "<hello>world</hello>" PS> $hwxml hello ----- world It is important to note that "int", "double", "string", "wmiclass", "xml" etc. are basically just aliases for the real .NET types: System.Int32 System.Double System.String System.Xml.XmlDocument System.Management.ManagementClass#ROOT\cimv2\Win32_DiskDrive etc. Also you should know that you can ommit "System." so that [Int32] and [Xml.XmlDocument] would also be correct. Many usefull .NET assemblies like System.XML are loaded by default. To load your own assemblies you can use the following command as a template: [Reflection.Assembly]::LoadFrom("C:\PoSh\Example.Test.dll") -- greetings dreeschkind "peterchen" wrote: > Is there something I have to load / register for the [xml] stuff? > > get-contents (according to | get-member) returns only a string, and I > can't find anything about xml using help > > thanks! > > klumsy@xtra.co.nz wrote: > > you do SOMETHING like this (just out of my head so sorry if there are > > mistakes) > > > > $a = [xml]get-content myfile.xml > > $a.rootnode.someothernode.othernodeyouwantochange = 'new data' > > $a > newfile.xml > > > > it probably won't run but get you started > > > > > > Peter Hauptmann wrote: > > > Is there a way to modify an XML file using powershell? I poked around > > > in the help and on google, but didn't find anything. > > > > > > I try to replace the contents of a single tag. The tag is unique and > > > formatting is consistent so I could also use a simple text > > > search-and-replace. > > > > > > Thanks! > > > ph > > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Modify XML file Now it all makes sense - and it even works! :-) dreeschkind wrote: > Hi, here are some more examples and a little explanation: > > PS>$var = [sometype] somevalue > > works like the cast operator in other languages. > So you can do things like this: > > PS> [int] "5" > 5 > > PS> [string] 5 > 5 > > PS> [double] "3.14159265358979" > 3.14159265358979 > > PS> [int] 3.14159265358979 > 3 > > PS> [wmiclass] "Win32_DiskDrive" > > Win32_DiskDrive > > PS> $hwxml = [xml] "<hello>world</hello>" > PS> $hwxml > > hello > ----- > world > > It is important to note that "int", "double", "string", "wmiclass", "xml" > etc. are basically just aliases for the real .NET types: > > System.Int32 > System.Double > System.String > System.Xml.XmlDocument > System.Management.ManagementClass#ROOT\cimv2\Win32_DiskDrive > etc. > > Also you should know that you can ommit "System." so that [Int32] and > [Xml.XmlDocument] would also be correct. > Many usefull .NET assemblies like System.XML are loaded by default. To load > your own assemblies you can use the following command as a template: > > [Reflection.Assembly]::LoadFrom("C:\PoSh\Example.Test.dll") > > -- > greetings > dreeschkind > > "peterchen" wrote: > > > Is there something I have to load / register for the [xml] stuff? > > > > get-contents (according to | get-member) returns only a string, and I > > can't find anything about xml using help > > > > thanks! > > > > klumsy@xtra.co.nz wrote: > > > you do SOMETHING like this (just out of my head so sorry if there are > > > mistakes) > > > > > > $a = [xml]get-content myfile.xml > > > $a.rootnode.someothernode.othernodeyouwantochange = 'new data' > > > $a > newfile.xml > > > > > > it probably won't run but get you started > > > > > > > > > Peter Hauptmann wrote: > > > > Is there a way to modify an XML file using powershell? I poked around > > > > in the help and on google, but didn't find anything. > > > > > > > > I try to replace the contents of a single tag. The tag is unique and > > > > formatting is consistent so I could also use a simple text > > > > search-and-replace. > > > > > > > > Thanks! > > > > ph > > > > |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Modify XML file As for your original question about modifying XML, you might find this helpful: http://blogs.msdn.com/powershell/arc...owerShell.aspx -- Lee Holmes [MSFT] Windows PowerShell Development Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "peterchen" <caligula@gmail.com> wrote in message news:1161180036.387559.244320@m73g2000cwd.googlegroups.com... > Now it all makes sense - and it even works! :-) > dreeschkind wrote: >> Hi, here are some more examples and a little explanation: >> >> PS>$var = [sometype] somevalue >> >> works like the cast operator in other languages. >> So you can do things like this: >> >> PS> [int] "5" >> 5 >> >> PS> [string] 5 >> 5 >> >> PS> [double] "3.14159265358979" >> 3.14159265358979 >> >> PS> [int] 3.14159265358979 >> 3 >> >> PS> [wmiclass] "Win32_DiskDrive" >> >> Win32_DiskDrive >> >> PS> $hwxml = [xml] "<hello>world</hello>" >> PS> $hwxml >> >> hello >> ----- >> world >> >> It is important to note that "int", "double", "string", "wmiclass", "xml" >> etc. are basically just aliases for the real .NET types: >> >> System.Int32 >> System.Double >> System.String >> System.Xml.XmlDocument >> System.Management.ManagementClass#ROOT\cimv2\Win32_DiskDrive >> etc. >> >> Also you should know that you can ommit "System." so that [Int32] and >> [Xml.XmlDocument] would also be correct. >> Many usefull .NET assemblies like System.XML are loaded by default. To >> load >> your own assemblies you can use the following command as a template: >> >> [Reflection.Assembly]::LoadFrom("C:\PoSh\Example.Test.dll") >> >> -- >> greetings >> dreeschkind >> >> "peterchen" wrote: >> >> > Is there something I have to load / register for the [xml] stuff? >> > >> > get-contents (according to | get-member) returns only a string, and I >> > can't find anything about xml using help >> > >> > thanks! >> > >> > klumsy@xtra.co.nz wrote: >> > > you do SOMETHING like this (just out of my head so sorry if there are >> > > mistakes) >> > > >> > > $a = [xml]get-content myfile.xml >> > > $a.rootnode.someothernode.othernodeyouwantochange = 'new data' >> > > $a > newfile.xml >> > > >> > > it probably won't run but get you started >> > > >> > > >> > > Peter Hauptmann wrote: >> > > > Is there a way to modify an XML file using powershell? I poked >> > > > around >> > > > in the help and on google, but didn't find anything. >> > > > >> > > > I try to replace the contents of a single tag. The tag is unique >> > > > and >> > > > formatting is consistent so I could also use a simple text >> > > > search-and-replace. >> > > > >> > > > Thanks! >> > > > ph >> > >> > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Modify the hosts file | Vista General | |||
| Re: HOW TO: Modify the way a file list looks. | PowerShell | |||
| Error message: insufficient privileges to modify this file | Vista General | |||
| Modify a text file | PowerShell | |||