0

I have the following powershell code. It basically creates a template, that I then use to populate with XML data.

$template = @'
<?xml version="1.0" encoding="UTF-8"?>
<list>
    <details>
        <cj></cj>
        <jn></jn>
        <en></en>
        <st></st>
        <et></et>
    </details>
</list>
'@

$template | Out-File template.xml -encoding UTF8

$xml = New-Object xml
$xml.Load("template.xml")

It works fine, but I was wondering if there was a cleaner way of doing this that doean't involve exporting it to a file and then loading that file back in.

e.g. is there a way of passing $template directly to the $xml (similar to $xml.Load($template) - which doesn't work)

I've had a look around the web, but can't find anything to help - am I just looking in the wrong places? or can it not be done?

1 Answer 1

1

(Edit: oops... I'm on powershell version 3.0! Cannot say if this works on version 2.0, but it should!)

I think this way works:

$template = @'
<?xml version="1.0" encoding="UTF-8"?>
<list>
    <details>
        <cj></cj>
        <jn></jn>
        <en></en>
        <st></st>
        <et></et>
    </details>
</list>
'@

$xml = New-Object xml
$xml.LoadXml( $template )
Sign up to request clarification or add additional context in comments.

1 Comment

cheers, it does indeed work perfectly. You'd think something as simple as that would be easy to find. . .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.