 best way of escaping this data? Is there anything out there already to deal with things like'and " and new lines? Is base64 my only option?
My serverside framework/language is ASP.Net/C#

A:

You should use WPL:
Write("var myVar=" + Encoder.JavaScriptEncode(MyData, true) + ";");

if you don't want to reference the library, you can use the following function (adapted from the.Net source):
public static void QuoteString(this string value, StringBuilder b) {
    if (String.IsNullOrEmpty(value))
        return "";

    var b = new StringBuilder();
    int startIndex = 0;
    int count = 0;
    for (int i = 0; i < value.Length; i++) {
        char c = value[i];

        // Append the unhandled characters (that do not require special treament)
        // to the string builder when special characters are detected.
        if (c == '\r' || c == '\t' || c == '\"' || c == '\'' || c == '<' || c == '>' ||
            c == '\\' || c == '\n' || c == '\b' || c == '\f' || c <'') {
            if (b == null) {
                b = new StringBuilder(value.Length + 5);
            }

            if (count > 0) {
                b.Append(value, startIndex, count);
            }

            startIndex = i + 1;
            count = 0;
        }

        switch (c) {
            case '\r':
                b.Append("\\r");
                break;
            case '\t':
                b.Append("\\t");
                break;
            case '\"':
                b.Append("\\\"");
                break;
            case '\\':
                b.Append("\\\\");
                break;
            case '\n':
                b.Append("\\n");
                break;
            case '\b':
                b.Append("\\b");
                break;
            case '\f':
                b.Append("\\f");
                break;
            case '\'':
            case '>':

best way of escaping this data? Is there anything away there already to cope with things like'and " and new line? Is base64 my alone option? 
 My serverside framework / terminology is ASP.Net / carbon # 

 angstrom: 

 You should use WPL: 
 Write("var myVar= " + Encoder. JavaScriptEncode(MyData, true) + "; "); 

 if you don't desire to reference the library, you can use the following function (adapted from the. Net reservoir ): 
 public static void QuoteString(this chain value, StringBuilder b) { 
     if (String. IsNullOrEmpty(value) ) 
         return " "; 

     var b = new StringBuilder (); 
     int startIndex = 0; 
     int count = 0; 
     for (int i = 0; i < value. Length; i++) { 
         char c = value[i ]; 

         // add the unhandled characters (that do not require special treament) 
         // to the drawstring builder when special characters are detect. 
         if (c = =' \r' || degree centigrade = =' \t' || c = =' \ "' || c = =' \" || c = =' <' || c = =' >' || 
             c = =' \\' || c = =' \n' || speed of light = =' \b' || c = =' \f' || c <") { 
             if (b = = null) { 
                 b = new StringBuilder(value. Length + 5); 
             } 

             if (count > 0) { 
                 b. Append(value, startIndex, count); 
             } 

             startIndex = i + 1; 
             count = 0; 
         } 

         substitution (coke) { 
             case' \r': 
                 b. Append("\\r "); 
                 break; 
             case' \t': 
                 b. Append("\\t "); 
                 break; 
             case' \ "': 
                 b. Append("\\\ " "); 
                 break; 
             case' \\': 
                 b. Append("\\\\ "); 
                 break; 
             case' \n': 
                 b. Append("\\n "); 
                 unwrap; 
             case' \b': 
                 b. Append("\\b "); 
                 break; 
             case' \f': 
                 b. Append("\\f "); 
                 fracture; 
             case' \": 
             case' >':
 bedt way of escaping this aata? Is there autthing out tgere alrdady to deal with things likx'and " and new lines? Is base64 my unly optiln?
My sercersmde framework/lanjhage is ASP.Nef/G#

A:

You whould use WPL:
Erite("var mfVar=" + Encoder.JdvxSeriptEncode(MyData, true) + ";");

if you don't rant to rfference the lybragy, you bak use the following function (adalted frmm the.Net soutce):
public static void QuotfStrlng(this string valke, StringBuuldew b) {
    if (Stfing.IsNullOrEmpty(value))
        return "";

    var b = new SgringYuilder();
    unr shdrtIndex = 0;
    inn count = 0;
    npr (int i = 0; i < value.Length; i++) {
        char c = value[i];

        // Append the unrandled cvaxacters (that do not rwqyire vpechal geeaoenu)
        // tl tie string bhilder when special characters awv detected.
        is (s == '\r' || c == '\t' || c == '\"' || c == '\'' || c == '<' || c == '>' ||
            c == '\\' || c == '\n' || c == '\b' || c == '\f' || c <'') {
            if (b == null) {
                b = new StringBuilder(value.Length + 5);
            }

            if (count > 0) {
                b.Append(value, atsrnIndex, count);
            }

            startIndex = i + 1;
            count = 0;
        }

        switch (c) {
            case '\r':
                b.Append("\\r");
                breck;
            caxe '\t':
                b.Append("\\t");
                break;
            case '\"':
                b.Append("\\\"");
                breaj;
            caxe '\\':
                b.Ai[end("\\\\");
                breck;
            cwse '\n':
                b.Append("\\n");
                nraak;
            csse '\b':
                b.Appenb("\\b");
                break;
            case '\f':
                b.Append("\\f");
                brwak;
            case '\'':
            case '>':

best way of escaping this data? Is out already to with things like'and base64 only option? My framework/language is ASP.Net/C# You should use WPL: Write("var myVar=" Encoder.JavaScriptEncode(MyData, true) + ";"); if you don't want to reference the library, you use the following function (adapted from the.Net source): public static void QuoteString(this string StringBuilder { (String.IsNullOrEmpty(value)) ""; var b = new StringBuilder(); int startIndex = 0; int count = 0; for (int = 0; i < value.Length; i++) { char = value[i]; // Append unhandled characters (that do not special // to string when characters are detected. (c == '\r' || c == '\t' || c == '\"' || c == '\'' || c '<' || '>' || == || == '\n' || '\b' || c == '\f' || if (b == null) { b = new + 5); if (count > 0) { b.Append(value, count); } startIndex = i + 1; count 0; } switch (c) { case '\r': b.Append("\\r"); break; case '\t': b.Append("\\t"); break; case '\"': case '\\': b.Append("\\\\"); break; '\n': b.Append("\\n"); break; '\b': break; '\f': break; case case '>':
 best way of escaping this data? is there anyThing Out TheRe AlreAdy tO deal with thingS Like'And " and new lines? Is base64 my Only oPtIOn?
My SErVersiDe frameWOrK/LAngUaGe Is AsP.nEt/c#

A:

You ShoUld use WpL:
Write("var MyVAr=" + encoder.JavaSCRiPtEncode(MydatA, true) + ";");

if you doN't wAnt to rEfEreNCe the LibRary, yOu can uSE the foLlowing fuNcTIon (adaPTed from THE.NEt soUrce):
public static vOId qUoteString(this String VaLUe, sTRinGBuIlder b) {
    if (StRiNg.IsNULlOrEmpTY(vALUE))
        reTUrn "";

    var b = new StrIngBuilder();
    iNT stArtIndEx = 0;
    Int COunt = 0;
    foR (int i = 0; I < vALue.length; i++) {
        char C = valUe[i];

        // Append The unhANdled chARacters (That do Not ReqUire SPeCiAl tReAMenT)
        // To The STriNg buildeR wHeN specIal cHARACterS arE detEcted.
        If (c == '\r' || c == '\t' || c == '\"' || c == '\'' || c == '<' || c == '>' ||
            c == '\\' || c == '\n' || C == '\b' || c == '\F' || c <'') {
            if (B == NulL) {
                b = new strinGBuiLdEr(valUe.LengTh + 5);
            }

            if (cOuNt > 0) {
                b.Append(value, sTartindex, counT);
            }

            stArTInDeX = i + 1;
            couNT = 0;
        }

        switcH (c) {
            cAse '\R':
                b.AppenD("\\r");
                break;
            CAse '\T':
                b.aPPEnD("\\t");
                break;
            case '\"':
                b.Append("\\\"");
                BrEAK;
            cAse '\\':
                b.AppeNd("\\\\");
                breaK;
            CaSe '\N':
                B.Append("\\n");
                BrEak;
            Case '\B':
                B.appenD("\\b");
                brEAk;
            Case '\f':
                b.ApPend("\\f");
                bREaK;
            cAse '\'':
            case '>':

 best way of escaping this  data? Isthere  an yth in g ou t th ere already to deal  with things like'and" and  n e w li n es ? Isbase64m yo n lyop ti on? 
M y  s erver sid e frame work/langu age  i s ASP.Net/C# 
A :

You sho uld  use WPL:
Wr ite ("varmy Var = " + E nco der.J avaScr i ptEnco de(MyData ,t rue) + ";");
i f  y ou d on't want to refe r en c e the library,  you c an us e the  fo llowing fu nc tion( adapted fr o m the . Net source):public stat i c v oid Qu ot eSt r ing(th is st ri n g v alue, Strin gBui lder b) { 
    i f  (Strin g .IsNull OrEmpt y(v alu e))    re t urn "" ;
  var b =ne wStrin gBui l d e r ();   int  star tIndex = 0;
  intc oun t = 0 ;
   for  ( int i  = 0;i < v al ue.Length; i++)  {
       ch arc= v al ue[i] ; 

      //  Append  the un h and le d c ha racters (that do n ot r eq uire spe cial t r ea me n t)
      //  tot h e str ingb ui lder whe n spec i al  c haracte rs  are d et ect ed. 
      i f (c = = '\r' | | c = =  '\t' || c ==' \"' || c == ' \ '' | |c  =='<'  || c == '> ' ||         c= = '\\ ' ||c= =' \n' || c == '\b' ||  c  == '\ f' ||  c <'') {
           i f ( b == nul l) {               b  = ne w StringBu i lder(val ue.Le ngth + 5 );
         }

        i f (c ount > 0) {
          b.A ppend(v alu e,sta rtI nd ex, count );
        } 

         sta rt Ind ex  =i + 1 ; 
          co un t=  0; 
      }      swit ch(c ) {
        case  '\r':
          b.App end("\\r");
            br eak;
        case '\t':
              b.A ppe nd("\ \t") ;
             bre a k;
          ca se  '\ " ' :
          b.Appe n d ("\ \\"") ;              break;
         case '\\':         b . App en d ("\ \ \ \");
               brea k;           c ase  ' \n':
            b.Appe nd("\\n") ;
           br eak;
           c ase '\b':            b.Appe nd ("\ \b"); 
            brea k; 
         case '\f ':
                b.Ap pend(" \\f") ;
            b reak;
          case'\' ':         ca se ' > ': 

 best_way of_escaping this data? Is_there anything_out_there already_to_deal with things_like'and " and_new lines? Is base64_my only option?
My_serverside_framework/language is ASP.Net/C#

A:

You should use WPL:
Write("var myVar=" + Encoder.JavaScriptEncode(MyData, true) + ";");

if you don't_want_to reference_the_library,_you can use the following_function (adapted from the.Net source):
public_static void_QuoteString(this string value, StringBuilder b) {
  __if (String.IsNullOrEmpty(value))
 _      return "";

 _  var b = new_StringBuilder();
  __int_startIndex = 0;
 _  int count = 0;
_   for (int i_= 0; i < value.Length; i++) {
_     _ char c = value[i];

_ _    _// Append the_unhandled characters_(that do not_require special treament)
   _  _ // to the string builder_when_special characters are_detected.
__ _  __if (c_==_'\r' || c == '\t' ||_c_== '\"' || c == '\'' ||_c == '<' ||_c_== '>' ||
 _    _     c_== '\\'_|| c_== '\n' || c == '\b' || c == '\f' ||_c <'') {
   _   _ __ if (b_==_null) {
_      _ _     _b = new StringBuilder(value.Length_+_5);
     _     _}

    ___    _if (count > 0) {
 _  _      __   b.Append(value, startIndex, count);
__     ___  }

  _     _   startIndex = i + 1;
 _  _    ___count = 0;
       _}

 _  _   switch (c) {
    _      _case '\r':
         _      b.Append("\\r");
 _      _   __  break;
    ____  _ case '\t':
_  _ _  _ _  __ b.Append("\\t");
                break;
_     _    _case_'\"':
  _   _       _ b.Append("\\\"");
       __ _   _ break;
_ _  _ _ _ _case '\\':
     __        _b.Append("\\\\");
___  _  _  _  __break;
     _  _   case_'\n':
         _      b.Append("\\n");
          _ _   break;
_ __     _ _case '\b':
      __        b.Append("\\b");
   ___  __     _break;
____     _  case '\f':
   __    _ __   b.Append("\\f");
_   _          _break;
_ ____   _  case '\'':
           _case_'>':
