0

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?

1408786user
  • 1,868
  • 1
  • 21
  • 39
  • 2
    Having a stray `>` would make it invalid XML. XML is not HTML. There is no way to put a single `>` in text without using XML constructs such as CDATA or an entity reference, which may break compatibility with HTML. – Matti Virkkunen Sep 15 '17 at 11:27
  • See this : https://msdn.microsoft.com/en-us/library/ee388354(v=vs.110).aspx – PaulF Sep 15 '17 at 11:49
  • Or this : https://stackoverflow.com/questions/5304311/unescaping-xml-entities-using-xmlreader-in-net – PaulF Sep 15 '17 at 11:50
  • @MattiVirkkunen Thanks, CData did the job. I added an answer with the code which worked for me. – 1408786user Sep 25 '17 at 10:25

1 Answers1

0

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> 
1408786user
  • 1,868
  • 1
  • 21
  • 39