I am performing XML Operations on an XHTML document utilizing Xml and Linq.
When converting it back to a String for outputting to a browser, it renders script tags in their original provided form when creating the XElement as
<script />
.
That prevents it being processed correctly by most browsers on the market.
I would like to perform the XElement to String conversion in a way that it outputs the script tag like this <script></script>
Can someone help me on that? Thanks in advance. :)
First edit
Providing some more information, the data is coming from an xml field in a MSSQL 2008R2 database.
It is loaded from the xml field as
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js" />
Second edit Working sample
using System;
using System.Linq;
using System.Xml.Linq;
namespace Test1
{
class Test1
{
static void Main(string[] args)
{
XElement element = XElement.Parse("<div><script /><script>Test</script></div>");
var EmptySet = (from e in element.DescendantsAndSelf()
where e.IsEmpty
select e);
foreach(XElement e in EmptySet)
{
e.Value = String.Empty;
}
Console.WriteLine(element.ToString(SaveOptions.None));
}
}
}
And its results
<div>
<script></script>
<script>Test</script>
</div>