Reputation: 29434
We want to assign an attribute whose contents already contain entities like "
or &
.
In this example, we want the title attribute to be Stack "Stacky" Overflow
:
$elem = $xml.CreateElement("Site");
$elem.SetAttribute("Title", "Stack "Stacky" Overflow");
But that turns into the following piece of XML output:
<Site Title="Stack &quot;Stacky&quot; Overflow" />
That behaviour is even stated in the documentation about the XmlElement.SetAttribute Method:
In order to assign an attribute value that contains entity references, the user must create an XmlAttribute node plus any XmlText and XmlEntityReference nodes, build the appropriate subtree and use SetAttributeNode to assign it as the value of an attribute.
Upvotes: 3
Views: 5134
Reputation: 1269
Dont know if this helps
Add-Type -AssemblyName System.Web
$elem = $xml.CreateElement("Site");
$elem.SetAttribute("Title",[System.Web.HttpUtility]::HtmlDecode("Stack "Stacky" Overflow"));
$elem.OuterXml
Upvotes: 1
Reputation: 126902
PS> $xml.Site.Title = [System.Security.SecurityElement]::Escape('Stack "Stacky" Overflow')
PS> $xml.Site.Title
Stack "Stacky" Overflow
Upvotes: 0
Reputation: 29434
$elem = $xml.CreateElement("Site");
$elemAttr = $xml.CreateAttribute("Title");
$elemAttr.InnerXml = "Stack "Stacky" Overflow";
$elem.SetAttributeNode($elemAttr);
XML output:
<Site Title="Stack "Stacky" Overflow" />
Upvotes: 4