By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,575 Members | 3,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,575 IT Pros & Developers. It's quick & easy.

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

P: n/a
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++

Nov 16 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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
"Brett Hofer" <bh****@centermarktech.com> wrote in message
news:BD75C9F4.345%bh****@centermarktech.com...
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++

Nov 16 '05 #2

P: n/a
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++
On 9/21/04 12:19 PM, in article
_v******************@newssvr19.news.prodigy.com, "Drebin"
<th*******@hotmail.com> wrote:
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
"Brett Hofer" <bh****@centermarktech.com> wrote in message
news:BD75C9F4.345%bh****@centermarktech.com...
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++



Nov 16 '05 #3

P: n/a
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" <bh****@centermarktech.com> wrote in message
news:BD75C9F4.345%bh****@centermarktech.com...
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++

Nov 16 '05 #4

P: n/a
"Brett Hofer" <bh****@centermarktech.com> wrote in message
news:BD75C9F4.345%bh****@centermarktech.com...
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.

---
Tom Tempelaere
Nov 16 '05 #5

P: n/a
but not for well-formed XML - must be double-quotes.. " not '
"TT (Tom Tempelaere)" <_/\/_0P@|/\|titi____AThotmailD.Tcom|/\|@P0_/\/_>
wrote in message news:Y9***********************@phobos.telenet-ops.be...
"Brett Hofer" <bh****@centermarktech.com> wrote in message
news:BD75C9F4.345%bh****@centermarktech.com...
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.

---
Tom Tempelaere

Nov 16 '05 #6

P: n/a
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++
On 9/21/04 2:12 PM, in article
Y9***********************@phobos.telenet-ops.be, "TT (Tom Tempelaere)"
<_//_0?P@|/|titi____AThotmailD.Tcom|/|@P?0_//_> wrote:
"Brett Hofer" <bh****@centermarktech.com> wrote in message
news:BD75C9F4.345%bh****@centermarktech.com...
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.

---
Tom Tempelaere


Nov 16 '05 #7

P: n/a
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++
On 9/21/04 2:25 PM, in article
3m****************@newssvr33.news.prodigy.com, "Drebin"
<th*******@hotmail.com> wrote:
but not for well-formed XML - must be double-quotes.. " not '
"TT (Tom Tempelaere)" <_/\/_0P@|/\|titi____AThotmailD.Tcom|/\|@P0_/\/_>
wrote in message news:Y9***********************@phobos.telenet-ops.be...
"Brett Hofer" <bh****@centermarktech.com> wrote in message
news:BD75C9F4.345%bh****@centermarktech.com...
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.

---
Tom Tempelaere



Nov 16 '05 #8

P: n/a
Brett Hofer <bh****@centermarktech.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

P: n/a
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" <bh****@centermarktech.com> wrote in message
news:BD75EB02.388%bh****@centermarktech.com...
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++
On 9/21/04 2:25 PM, in article
3m****************@newssvr33.news.prodigy.com, "Drebin"
<th*******@hotmail.com> wrote:
but not for well-formed XML - must be double-quotes.. " not '
"TT (Tom Tempelaere)" <_/\/_0P@|/\|titi____AThotmailD.Tcom|/\|@P0_/\/_> wrote in message news:Y9***********************@phobos.telenet-ops.be...
"Brett Hofer" <bh****@centermarktech.com> wrote in message
news:BD75C9F4.345%bh****@centermarktech.com...
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.
---
Tom Tempelaere


Nov 16 '05 #10

P: n/a
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!)
"Brett Hofer" <bh****@centermarktech.com> wrote in message
news:BD75C9F4.345%bh****@centermarktech.com...
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++

Nov 16 '05 #11

P: n/a
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++
On 9/21/04 2:47 PM, in article
MP************************@msnews.microsoft.com, "Jon Skeet [C# MVP]"
<sk***@pobox.com> wrote:
Brett Hofer <bh****@centermarktech.com> wrote:
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.


Nov 16 '05 #12

P: n/a
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

"Brett Hofer" <bh****@centermarktech.com> wrote in message
news:BD75F81F.3A0%bh****@centermarktech.com...
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++
On 9/21/04 2:47 PM, in article
MP************************@msnews.microsoft.com, "Jon Skeet [C# MVP]"
<sk***@pobox.com> wrote:
Brett Hofer <bh****@centermarktech.com> wrote:
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.

Nov 16 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.