Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Modify XML file

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 10-09-2006   #1 (permalink)
Peter Hauptmann
Guest


 

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 SpecsSystem Spec
Old 10-09-2006   #2 (permalink)
klumsy@xtra.co.nz
Guest


 

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 SpecsSystem Spec
Old 10-18-2006   #3 (permalink)
peterchen
Guest


 

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 SpecsSystem Spec
Old 10-18-2006   #4 (permalink)
ClaudioG64
Guest


 

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 SpecsSystem Spec
Old 10-18-2006   #5 (permalink)
dreeschkind
Guest


 

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 SpecsSystem Spec
Old 10-18-2006   #6 (permalink)
peterchen
Guest


 

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 SpecsSystem Spec
Old 10-18-2006   #7 (permalink)
Lee Holmes [MSFT]
Guest


 

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 SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: HOW TO: Modify the way a file list looks. Kiron PowerShell 3 12-09-2007 03:55 PM
Error message: insufficient privileges to modify this file Philly lawyer Vista General 6 10-03-2007 02:50 PM
Modify CSV, add fields BZP PowerShell 6 08-10-2007 03:58 PM
Modify a text file CGM PowerShell 4 07-15-2007 04:37 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008