Zirono
Zirono

Reputation: 89

Retrieve XML content of a variable

I used to have an XML file that was populating a variable ([xml]$modificationData = Get-Content $modificationXmlFileName) but now I am unable to store the file and must put it into a a different datafile, the format that I was told to do this in is:

xmldata = @'
  <bigSection>
    <smallSection>
      <changes>
      </changes>
    </smallSection>
  </bigSection>
'@

I'm not sure if the Get-Content cmdlet would work now if I rout it to this new variable and I'm not entirely sure what else I could use.

Also, I don't know what the @''@ wrapping around the code means. I thought the @ symbol was used for arrays but I dont think that it does that in this case.

Any clarification would be helpful.

Upvotes: 1

Views: 729

Answers (2)

briantist
briantist

Reputation: 47792

The @'...'@ syntax is called a here string or a here document. It's a way of specifying a string without having to escape characters.

Get-Content is for reading files. When you call it by default, it returns an array of lines, unless you call it with the -Raw parameter (and then it would return one big string with all the content of the file).

So in your example, $xmldata is the same as if you had called Get-Content $modificationXmlFileName -Raw.

Since you're just trying to cast to [xml], you can just do that:

$xmldata = [xml]@'
...
'@

Upvotes: 2

Martin Brandl
Martin Brandl

Reputation: 58931

The '@' @' Syntax is called a here-string which represents a multi-line string with no interpolation.

You don't have to / can't use the Get-Content cmdlet using the variable, instead just cast it to xml:

[xml]$modificationData  = xmldata

Upvotes: 1

Related Questions