Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read CDATA in XML file with PowerShell?

I am having a difficult time reading an XML file with Cdata inside.

in $xmlsource

<list>
  <topic>
     <topicTitle>Test</topicTitle>
     <topicDetail><![CDATA[<br>randomHTMLhere</br>]]></topicDetail>
  </topic>
</list>

powershell

[xml]$xml = get-content $xmlsource    

foreach ($topic in $xml.list) {
    $topic.topicTitle
    $topic.topicDetail
}

$topic.topicDetail will be null while $topic.topicTitle will not be. Any ideas?

like image 947
puttputt Avatar asked Aug 13 '09 19:08

puttputt


People also ask

How do I use CDATA in XML?

Syntax. CDATA End section − CDATA section ends with ]]> delimiter. CData section − Characters between these two enclosures are interpreted as characters, and not as markup. This section may contain markup characters (<, >, and &), but they are ignored by the XML processor.

Can PowerShell read XML?

PowerShell and Reading XML filesPowerShell provides an easy way to read XML files, manipulate them and finally to save them back to disk without writing a lot of code or knowing XPath. PowerShell does this by providing the user dot notation to signify each node in the XML document.

What does <![ CDATA in XML mean?

A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.

Can we use CDATA in XML attribute?

No, The markup denoting a CDATA Section is not permitted as the value of an attribute.


1 Answers

$topic.topicDetail."#cdata-section"

or

$topic.topicDetail.InnerText

like image 66
Peter Seale Avatar answered Sep 21 '22 13:09

Peter Seale