locked
Help with Formatting XML RRS feed

  • Question

  • Hey Everyone I'm trying to define some XML in Powershell. Here's what I have so far which works but I have some issues:

    $XML = [xml]@"
    <?xml version="1.0" encoding="UTF-8"?>
    <r25:spaces xmlns:r25="http://www.collegenet.com/r25">
        <r25:space crc="$($spaceResults.spaces.space.crc)" status="mod">
            <r25:space_id>$($space_id)</r25:space_id>
            <r25:space_name>$($space_name)</r25:space_name>
            <r25:formal_name>$($formal_name)</r25:formal_name>
            <r25:max_capacity>$($max_capacity)</r25:max_capacity>
        </r25:space>
    </r25:spaces>
    "@

    So this works fine and I do it like this so I can use variables inside it but when ever I try to tab it over the ISE says that it's wrong. It looks like if I want to use "@@" I can't tab it over? I tried using double and single quotes but that won't work. Any suggestions?

    Thanks.

    Wednesday, August 2, 2017 12:25 PM

Answers

  • $xml.spaces.space.spaceid = 'comevalue'

    This works for the text content of any node.  Attributes can also be populated this way.

    XML formatting is set by the provider.  You cannot change it without rewriting the whole XML.

    TO save it with formatting you must use the $xml.Save(...) method


    \_(ツ)_/

    • Marked as answer by Cody_H Wednesday, August 2, 2017 7:45 PM
    Wednesday, August 2, 2017 5:16 PM

All replies

  • What do you mean when you try to tab it over? The code posted above works without issue.

    With here-strings, you can save to a variable or just create the string

    $var = @"

    some text

    "@

    or

    @"

    some text

    "@

    You cannot have white space in front of @" or "@


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Wednesday, August 2, 2017 1:50 PM typo
    Wednesday, August 2, 2017 1:48 PM
  • Thanks for the reply by tab it over I mean I hit the tab button and the text goes to the right. I do this in loops to make it easier to read. I think the issue is the tab is considered whitespace. Is there anyway to populate an XML variable with other variables or do you have to use a here-string?
    Wednesday, August 2, 2017 5:12 PM
  • $xml.spaces.space.spaceid = 'comevalue'

    This works for the text content of any node.  Attributes can also be populated this way.

    XML formatting is set by the provider.  You cannot change it without rewriting the whole XML.

    TO save it with formatting you must use the $xml.Save(...) method


    \_(ツ)_/

    • Marked as answer by Cody_H Wednesday, August 2, 2017 7:45 PM
    Wednesday, August 2, 2017 5:16 PM
  • Thanks this is what I ended up with:

    [xml]$Doc = New-Object System.Xml.XmlDocument
        $dec = $Doc.CreateXmlDeclaration("1.0","UTF-8",$null)
        $Doc.AppendChild($dec) | Out-Null
        $root = $doc.CreateNode("element","r25:spaces",$null)
        $root.SetAttribute("xmlns:r25","http://www.collegenet.com/r25")
     
        $d = $doc.CreateNode("element","r25:space",$null)
        $d.SetAttribute("status","mod")
        $d.SetAttribute("crc",$spaceResults.spaces.space.crc)
    
        $e = $doc.CreateNode("element","r25:space_id",$null)
        $e.InnerText = $space_id
        $d.AppendChild($e) | Out-Null
    
        $f = $doc.CreateNode("element","r25:formal_name",$null)
        $f.InnerText = $formal_name
        $d.AppendChild($f) | Out-Null
    
        $g = $doc.CreateNode("element","r25:max_capacity",$null)
        $g.InnerText = $max_capacity
        $d.AppendChild($g) | Out-Null
    
        $root.AppendChild($d) | Out-Null
        $Doc.AppendChild($root) | Out-Null

    Wednesday, August 2, 2017 7:45 PM