Unwanted Escape Codes In String...

S

Steve Litvack

Hello,

I have built an XMLDocument object instance and I get the following string
when I examine the InnerXml property:

<?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1
QID=\"55111\"><Tag2 AID=\"5511101\"></Tag2></Tag1><Tag1 QID=\"55112\"><Tag2
AID=\"5511217\"></Tag2></Tag1><Tag1 QID=\"5512282\"><Tag2
AID=\"551228206\"></Tag2></Tag1><Tag1 QID=\"55114\"><Tag2
AID=\"5511406\"></Tag2></Tag1><Tag1 QID=\"55115\"><Tag2
AID=\"5511505\"></Tag2></Tag1></UserData></ROOT>

Notice that the double quotes are all escaped--that is they appear as

\"

versus

"

I would like to "transform" or "convert" this string into a string in which
the escape characters are "converted". This would generate a string that
would appear like this:

<?xml version="1.0"?><ROOT><UserData UserID="2282"><Tag1 QID="55111"><Tag2
AID="5511101"></Tag2></Tag1><Tag1 QID="55112"><Tag2
AID="5511217"></Tag2></Tag1><Tag1 QID="5512282"><Tag2
AID="551228206"></Tag2></Tag1><Tag1 QID="55114"><Tag2
AID="5511406"></Tag2></Tag1><Tag1 QID="55115"><Tag2
AID="5511505"></Tag2></Tag1></UserData></ROOT>

I do not want to use a search/replace algorithm because I cannot be entirely
sure that there will not be escape sequences other than \" in the string. I
am actually seeking a solution that would "transform" the \" to " (as I
require) but also one that would convert \n to ASCII 10 and \r to ASCII 13.
In other words, I would like to convert the escape-encoded string to a
standard ASCII string.

Moreover, why does the XMLDocument's InnerXml return an escape-encoded
string instead of a "straight" (unencoded) string? Is there a way to get the
InnerXml to return the "straight" string? I plan to pass the XML string as
an argument to a Sql stored procedure; therefore, the encoded string does
not work--I must use the unencoded version. Certainly others have
encountered this problem--I just could not find a single solution and I
spend hours searching MSDN and Google! For example, I read some other
threads that suggest using ActiveXMessageFormatter, but not one of them
actually explained how to do it--and pseudocode can only be so useful...

Thank you in advance for any assistance. I will confirm whether any
suggestions work.

Regards,
Steve
 
D

dd

It is the C# escape character, i.e. "\", as well as C and C++.
You do not have to replace it with anythng. Just leave it.
Just pass it to your stored procedure as is, it will work OK.
 
D

dd

Your problem is not the escape character, and it has nothing to do with
"pure" ASCII XML text. If your stored procedure has no errors and you are
passing your parameter properly, it should work without any problems. Here
is the code that I tried and it worked.

private SqlParameter CreateParameter(string sName, SqlDbType lType, int
iSize, ParameterDirection lDir,

bool bNullable, string sValue)

{

SqlParameter oParameter = new SqlParameter(sName, lType);

oParameter.Size = iSize;

oParameter.Direction = lDir;

oParameter.IsNullable = bNullable;

oParameter.Value = sValue;

return oParameter;

}

private void button1_Click(object sender, System.EventArgs e)

{

string sString = "<?xml version=\"1.0\"?><ROOT><UserData
UserID=\"2282\"><Tag1 QID=\"55111\"><Tag2
AID=\"5511101\"></Tag2></Tag1><Tag1 QID=\"55112\"><Tag2
AID=\"5511217\"></Tag2></Tag1><Tag1 QID=\"5512282\"><Tag2
AID=\"551228206\"></Tag2></Tag1><Tag1 QID=\"55114\"><Tag2
AID=\"5511406\"></Tag2></Tag1><Tag1 QID=\"55115\"><Tag2
AID=\"5511505\"></Tag2></Tag1></UserData></ROOT>";

SqlConnection oConn = new SqlConnection();

SqlCommand oCmd = new SqlCommand();

oConn.ConnectionString = "your connection string";

try{oConn.Open();}

catch(Exception ex){string sException = ex.Message;};

oCmd.Connection = oConn;

oCmd.CommandType = CommandType.StoredProcedure;

oCmd.CommandText = "sp_Test";

oCmd.Parameters.Add(CreateParameter("@StringIn", SqlDbType.VarChar, 900 ,
ParameterDirection.Input,

false, sString));

try

{

oCmd.ExecuteNonQuery();

}

catch(System.Exception ex){string sException = ex.Message;}

oConn.Close();

oCmd.Dispose(); oConn.Dispose();

}



Here is the stored provedure:

CREATE PROCEDURE sp_Test

@StringIn varchar(900)
AS
DECLARE @hdoc int
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @hdoc OUTPUT, @StringIn
EXEC sp_xml_removedocument @hDoc

GO
 
S

Steve Litvack

OK... This obviously is one of those problems where the stored proc works
with "well-formed" (what I call "pure") XML yet does not work with XML
containing escape sequences. This made it appear to be a problem with the
XML I'm using. However, thanks to your efforts you have convinced me that
the problem can also be fixed by reviewing the stored proc and leaving my
XML as-is. Thus, this problem seems to have two solutions: change the XML or
change the sproc.

I will do a little more research on this issue, study what you have kindly
written (and thank you for taking the time you clearly took), and I will
reply with either more information or with the solution(s) I have found.

Regards,
-- Steve
 
D

dd

Steve,
If you pass your string, which is a string that contains escape codes, using
Command Parameters it will work OK.
The escape characters will be taken care of by the parameter object, and the
string will be passed to the stored procedure as it is supposed to be, i.e.
no escape characters will be passed to the stored procedure. Consequently,
XMLDoc object inside your stored procedure will take it. Just use
parameters, that's all.
 
S

Steve Litvack

Thank you again for your help, but when we try your example we still get the
parsing error. Perhaps you have a difference SQL Server setting or DB
setting from us? We're using Sql Server 2000.

Regards,
-- Steve
 
S

Steve Litvack

OK--I will try your exact code and will let you know (this message follows
my other recent message that reported that we're still getting errors).

Thanks,
Steve
 
D

dd

I am running SQL Server 2000.
If you want you can post your code and the stored procedure. I will run it
on my machine, and we'll se what happens.
 
S

Steve Litvack

Hello:

Well--you were right all along (I'm man enough to bow to you and apologize
for my earlier insistence that the escape codes were causing the problem).
There are two conclusions here:

1. Passing a C# string containing escape codes does indeed work. The reason
it did NOT work had to do with the fact that my XML tags were in mixed case
and the sproc was expecting all uppercase tag names. I suppose that the Sql
OLEDB driver converts C strings to Sql-compliant strings and effectively
removes the escape sequences?

2. When I was getting the error from SQL Server, I was running the sproc
from Query Analyzer and passing the escape-coded string there. Apparently,
Query Analyzer doesn't like such strings and generates a parsing error.

I discovered the problem by trying the XML string in Query Analyzer without
the escape codes. I did not get an error from the sproc, but the insert it
attempts was not done. Once I realized the XML case issue, I corrected it
and then sproc worked. Then I added back the escape codes and it failed in
Query Analyzer. However, the string with the corrected XML tags and with
escape codes worked when passed as a parameter from .Net.

I offer you my most sincere thanks for your persistence in helping to solve
this problem. You obviously took a lot of time to help, and I am most
greatful.

The only remaining question is: Why are those escape codes appearing in my
XML string? At this point this is obviously just an academic question...
However, I would be interested to know how to ensure that they don't appear
even though they are no longer an issue for Sql Server.

Thanks again...

Regards,
-- Steve
 
C

codewriter

No prob.

The escape codes are intrinsic to C#, as well as to C and C++. Compiler
thinks that a double quote is a beginning or an end of a string. If there is
an escape character, the complier treats the following character
differently. The reason you see the escape character is that you are looking
at the string from within C# IDE. For example, try to save a string to a
text file, you will not see any escape characters:
string sString = "abc\"def";
StreamWriter oWriter = File.CreateText("E:\\aaa.txt");
oWriter.Write(sString);
oWriter.Close();
 
Top