Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Modify XML file

Reply
 
Old 10-09-2006   #1 (permalink)
Peter Hauptmann


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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]


 
 

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
Reply

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


Vista Forums 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 Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46