简体   繁体   中英

How can I add CData as a XElement Value in Powershell?

I've got a PS script that creates some XML and assigns values to the XElements. I need the value of the XElement to be wrapped in CData. This is using System.Xml.Linq I tried this:

$newNode.Element("details").Value = '<![CDATA[Traceback:'+$_.Exception.toString()+']]>'

but when I output the xml, it converts the '<' and '>' to &lt and &gt.

Add an object of type XCData to your element

[Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq") | Out-Null

[System.Xml.Linq.XCData] $cdata = New-Object -TypeName System.Xml.Linq.XCdata -ArgumentList "data"
[System.Xml.Linq.XElement] $element = New-Object -TypeName System.Xml.Linq.XElement -ArgumentList "test", $cdata

Give this a try:

$cdata = $xml.CreateCDataSection($content)
$parent = $xml.GetElementsByTagName("TagName")[0]
$parent.AppendChild($cdata)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM