Search code examples
javascriptc#xmlxelement

javascript in XElement


What is the correct way to put javascript in an XElement object in C#?

Currently I have the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ConsoleApp2
{
   class Program
   {
      static void Main(string[] args)
      {
         var tag = new XElement
                (
                   "script",
                   new XAttribute("type", @"text/javascript"),
                   @"

    $().ready(onLoad);

    function onLoad()
    {
       if (3 > 1){
         alert('Hello world');
       }
    };
    "
                );

         Console.WriteLine(tag.ToString());

         Console.ReadKey();
      }
   }
}

Which gives as output:

<script type="text/javascript">

    $().ready(onLoad);

    function onLoad()
    {
       if (3 &gt; 1){
         alert('Hello world');
       }
    };
    </script>

But instead of &gt; I actually want >... What am I doing wrong? Is there a correct way to put javascript in an XElement? If not what's the right way?


Solution

  • The following worked. I had to add XCData and put // in front of the XCData element and also // at the end of the javascript.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    using System.Web;
    
    namespace ConsoleApp2
    {
       class Program
       {
          static void Main(string[] args)
          {
             var tag = new XElement
                    (
                       "script",
                       new XAttribute("type", @"text/javascript"),
                       "//",
                       new XCData(@"
    
        $().ready(onLoad);
    
        function onLoad()
        {
           if (3 > 1){
             alert('Hello world');
           }
        };//")
                    );
    
    
             Console.WriteLine(tag.ToString());
    
             Console.ReadKey();
          }
       }
    }
    

    Which gives the following output:

    <script type="text/javascript">//<![CDATA[                                                                                                                                                                                                              
        $().ready(onLoad);                                                                                                                                                                                                                              
           function onLoad()                                                                                                       
           {                                                                                                                          
              if (3 > 1){                                                                                                               
                  alert('Hello world');                                                                                                 
              }                                                                                                                    
           };//]]></script>