Removing \t\n stuff from XML

Hi all,

im building a super cool Reporting Server in ASP.NEt that parses XML docs and generates sql to run and return results. And i must say, all you MSD's will be impressed. However i need some help still.

My Xml sturcture is (very) basically like this

...

<query>
<sql>

My SQL HERE

</sql>

</query>

i Preserve the format in the sql block and so when i pull the sql text from the XML doc i get some \t\n and \r\n line returns in the text that comes back. How can i use Regex (or Replace()) to remove these from the strings. I am having a hard time finding a Regex pattern that will remove this from the strings i am making.

Thanks a lot

mcm




Answer this question

Removing \t\n stuff from XML

  • Viorel.

    queryString=queryString.Replace("\n","");
    queryString=queryString.Replace("\t","");
    queryString=queryString.Replace("\r","");

    Or you can make a function that takes a String, a Char[], and a Char as parameters and replaces all occurences of all elements of the Char[] in the String with the Char:

    Public Static String MultiReplace(String base, Char[] search, Char replace)
    {

    String tmp=base.Clone();
    for (int i=0;i<search.Length();i++)
    {

    tmp=tmp.Replace(search[ i ],replace);

    }
    return tmp;

    }

    Then you call MultiReplace(queryString, new Char[]({'\n','\t','\r'}));

     



  • Kalexin

    string s = @"select *

    "+"\t  from customer"+@"

         where zip = 98077";

    Regex regex = new Regex("[ \t\r\n]+");

    string result = regex.Replace(s, " ");

    Console.WriteLine(result);

    Produces:

    "select * from customer where zip = 98077"

    PS: watch out for SQL injection security holes:

    http://msdn.microsoft.com/msdnmag/issues/04/09/SQLInjection/


  • Removing \t\n stuff from XML