Double Quotes in C# - Prob something simple but I feel im goingcrazy

  • Thread starter Thread starter Brett Hofer
  • Start date Start date
B

Brett Hofer

I must be missing something - Im a veteran C++ programmer now working with
C# overall I like the language but find many weird changes... Anyway Im
writing code behind an aspx. In this one C# method I am building an XML
string to be insterted into a database. This string should result in:
<row FIELD1="value1" FIELD2="value2" \>

I am using a string type variable and I cannot get the double quotes to be
added properly I have tried all of the things that are supposed to work such
as:

string sXML;

sXML = "<row ";

// Ive tried this
sXML += "FIELD1=\"value1\" ";
sXML += "FIELD2=\"value2\" \\>";

// and this
sXML += @"FIELD1=""value1"" ";
sXML += @"FIELD2=""value2"" \\>";

// and this
sXML += "FIELD1=\u0022value2\u0022 ";
sXML += "FIELD2=\u0022value2\u0022 \\>";

In all cases when I examine these cases the sXML string and/or the value
inserted into the database results in:
<row FIELD1=\"value1\" FIELD2=\"value2\" \\>


Why are the \ showing up in the result string? Its things like this that
were so straight forward in C++ that drive me nuts in C# what am I missing
?

Brett++
 
First, the self-closing XML tag should be <myelement /> *not* <myelement
\> - this may not jam you up now, but will likely later!!

As for this other thing, it very well may be that what you are seeing, is
how various other programs/libraries/SQL store these special characters. The
following is proper syntax to do what you want:

stirng strXML = "<row value=\"data is here\" />";

If you were to print that, it would result in:

<row value="data is here" />

Try outputting it to the screen or someplace that you know will render
special characters. Just a thought.. hth
 
Good point on the ending element fixed that thanks.. However I thought the
compiler is supposed to translate \" to byte code 0x0022 .. I see the
result in several places such as the command window while debugging, the
tool tip when hovering over the variable and the value in the inserted
column? Ive done this exact same process in C++ many times... Its really
strange.. But I will try outputting it to the console and see what happens..
Problem is that even if it displays correctly I still need it to be stored
in the database without the \.. The table im inserting into already has
thousands of other records in it inserted correctly by other applications
and require properly formed XML..

Brett++
 
You also might consider using an XmlTextWriter to construct the XML.
Concatenating strings have sufficient overhead since string objects are
being created and destroyed with every join.
 
Brett Hofer said:
I must be missing something - Im a veteran C++ programmer now working with
C# overall I like the language but find many weird changes... Anyway Im
writing code behind an aspx. In this one C# method I am building an XML
string to be insterted into a database. This string should result in:
<row FIELD1="value1" FIELD2="value2" \>

I am using a string type variable and I cannot get the double quotes to be
added properly I have tried all of the things that are supposed to work such
as:

string sXML;

sXML = "<row ";

// Ive tried this
sXML += "FIELD1=\"value1\" ";
sXML += "FIELD2=\"value2\" \\>";

// and this
sXML += @"FIELD1=""value1"" ";
sXML += @"FIELD2=""value2"" \\>";

// and this
sXML += "FIELD1=\u0022value2\u0022 ";
sXML += "FIELD2=\u0022value2\u0022 \\>";

In all cases when I examine these cases the sXML string and/or the value
inserted into the database results in:
<row FIELD1=\"value1\" FIELD2=\"value2\" \\>


Why are the \ showing up in the result string? Its things like this that
were so straight forward in C++ that drive me nuts in C# what am I missing
?

Brett++

You can use single quotes instead of double quotes if i'm not mistaking.
 
Funny that's not the answer I expected but Ill take it to move forward :)..
Although since this XML string ends up in a SQL insert statement it may
require doubling up on all of the single quotes I think.. *ponders*

Im still hoping to find the cause of the underlying problem. Im wondering if
this problem is being cause by some unicode / ascii compiler setting. It
just baffles me that I cannot get this string to construct cleanly with
double quotes in it...

Thanks for the Tip!

Brett++
 
Actually according to my XML specs either ' or " on an attribute value is
considered well-formed... But would still like to uncover the real cause of
my issue...

Brett++


but not for well-formed XML - must be double-quotes.. " not '
 
Brett Hofer said:
Funny that's not the answer I expected but Ill take it to move forward :)..
Although since this XML string ends up in a SQL insert statement it may
require doubling up on all of the single quotes I think.. *ponders*

If it ends up as a value in a SQL statement, it should be encapsulated
in a parameter anyway. You shouldn't need to do any quoting in your
SQL.
Im still hoping to find the cause of the underlying problem. Im wondering if
this problem is being cause by some unicode / ascii compiler setting. It
just baffles me that I cannot get this string to construct cleanly with
double quotes in it...

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that looking in the debugger isn't a good start - write the
results out to the console, otherwise you may well get confused by the
debugger "helpfully" displaying extra backslashes etc.
 
Wow, I would've swore on my life that was incorrect and that I read
something to the opposite - but I stand corrected!
http://www.w3.org/TR/REC-xml/

Good one!

Brett Hofer said:
Actually according to my XML specs either ' or " on an attribute value is
considered well-formed... But would still like to uncover the real cause of
my issue...

Brett++
 
Given the following code:
public class MyClass
{
public static void Main()
{
string sXML = "<row ";
sXML += "FIELD1=\"value1\" ";
sXML += @"FIELD2=""value2"" \\>";
Console.WriteLine(sXML);
}

The output is:
<row FIELD1="value1" FIELD2="value2" \\>

Which is what you want (except for the double \\ at the end)

The <row FIELD1=\"value1\" FIELD2=\"value2\" \\\\> you are seeing is purely
the effect of the debugger, which is adding the slashes to make clear what
actually there. (Note the it includes a opening & closing quote). Put "\x0"
in the middle of a string and look at the debugger window and it makes more
sense.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
I was just about to post the method but I decided I should really confirm it
was being inserted into the DB the way the debugger was displaying the
string.. Turns out early on when I first went to verify that the data was
being written to the DB that way it had been right after I copied the full
string from the debugger command window into a SQL query window and ran it
.... Not from letting the full method complete the SQL execution with the
string...

Anyway ATTENTION ALL C++ PROGRAMMERS!!! Unlike the debuggers in C++ the C#
debugger displays the escape sequences even though they don't really exist
when using them!!! ARGG

Thank you all for you assist in this I promise my next question won't be so
clumsy... *NOTE TO SELF* Debugger displays all the escape sequences

Brett++
 
Hi Brett,

Yes this is a confusing pain in the b especially if you want to cut and
paste debugger displays into other tools. Surely if MS had to invent such a
dumb feature they could have at least made it optional.

Cheers

Doug Forster
 
Back
Top