XML Comments with PowerShell

by robertskinner at 2013-03-15 07:54:08

I am new to using XML with PowerShell, so I am not sure why this occurs, it could be my limited understanding. However, when creating an XML file with XML Notepad, I was given the option of adding comments as a Child. The first example works, but not the second example. Is this a limitation of XML or how I handle it in PowerShell. I am assuming that since the data is not closed before the comment, this is a problem. Just curious as to why this is an option in the XML creater if it is bad practice to do this.

I am calling the xml file from a commandline and importing the values:


[xml]$xml=Get-Content -Path "$args"
$ftpdata = $xml.main.ftp.datapath



This works when I pull the data from the xml file


<main>
<ftp>
<datapath>c:\code\data&lt;/datapath>
<!–The Location Directory of the File to be transported example: \server\d$\somedir\ -->
</ftp>
</main>



This does not work. The postion of the comment breaks this.


<main>
<ftp>
<datapath>c:\code\data<br> <!–The Location Directory of the File to be transported example: \server\d$\somedir\ -->
</datapath>
</ftp>
</main>
by ArtB0514 at 2013-03-15 08:14:09
It looks like the XML in your first example is invalid. It contains a second </datapath> without a corresponding <datapath>.
by robertskinner at 2013-03-15 08:26:34
That was a typo, I removed it. The first example is the one that does work.
by jonhtyler at 2013-03-15 09:32:51
For your second scenario, try this:

$ftpdata."#text"
by robertskinner at 2013-03-15 10:55:04
That worked, thanks :slight_smile: