C#中的双引号 - 简单的东西,但我觉得我很疯狂 [英] Double Quotes in C# - Prob something simple but I feel im goingcrazy

查看:104
本文介绍了C#中的双引号 - 简单的东西,但我觉得我很疯狂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定是在遗漏一些东西 - 我是一位经验丰富的C ++程序员,现在正在使用

C#整体我喜欢这种语言,但发现了许多奇怪的变化......无论如何我是

写作aspx背后的代码。在这个C#方法中,我正在构建一个XML

字符串,用于插入数据库。该字符串应该导致:

< row FIELD1 =" value1" FIELD2 = QUOT;值2" \>


我正在使用字符串类型变量而我无法获得双引号

正确添加我已经尝试了所有的东西我应该这样工作

如下:


string sXML;


sXML ="< row" ;;


//我试过这个

sXML + =" FIELD1 = \" value1 \" " ;;

sXML + =" FIELD2 = \" value2 \" \\>" ;;


//这个

sXML + = @" FIELD1 ="" value1"" " ;;

sXML + = @" FIELD2 ="" value2"" \\>" ;;


//这个

sXML + =" FIELD1 = \ u0022value2 \ u0022";

sXML + =" FIELD2 = \ u0022value2 \ u0022 \\>" ;;


在所有情况下,当我检查这些案例时sXML字符串和/或插入数据库的值

导致:

< row FIELD1 = \" value1 \" FIELD2 = \" value2\" \\>

为什么结果字符串中会显示\?像C ++这样的东西是如此简单直接用C语言让我疯狂#我错过了什么




Brett ++

解决方案

首先,自动关闭的XML标签应该是< myelement /> *不*< myelement

\> - 这可能不会让你现在堵塞,但很可能会在以后!!


至于另一件事,很可能就是你所看到的,是

各种其他程序/库/ SQL如何存储这些特殊字符。

以下是正确的语法来做你想做的事情:


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


如果你打印它,它会导致:


< row value =" data在这里 />


尝试将其输出到屏幕或您知道将呈现的某个地方

特殊字符。只是一个想法... hth

" Brett Hofer" < BH **** @ centermarktech.com>在消息中写道

新闻:BD75C9F4.345%bh **** @ centermarktech.com ...

我必须遗漏一些东西 - 我是一位经验丰富的C ++程序员现在正在工作总的来说,我喜欢C#语言,但却发现了许多奇怪的变化......无论如何我在
后面编写代码。在这个C#方法中,我正在构建一个XML
字符串,以便插入到数据库中。该字符串应该导致:
< row FIELD1 =" value1" FIELD2 = QUOT;值2" \>

我正在使用字符串类型变量,我无法正确添加双引号我已经尝试了所有应该工作的东西
例如:

字符串sXML;

sXML ="< row" ;;

//我试过这个
sXML + =" FIELD1 = \" value1 \" " ;;
sXML + =" FIELD2 = \" value2 \" \\>" ;;

//这个
sXML + = @" FIELD1 ="" value1"" " ;;
sXML + = @" FIELD2 ="" value2"" \ \>" ;;

//这个
sXML + =" FIELD1 = \\\002222 = 00022" ;;
sXML + =" FIELD2 = \\ 00002 = 2> \ 0000 \\\ \\>>" ;;

在我检查这些情况的所有情况下,sXML字符串和/或插入到数据库中的值
结果in:
< row FIELD1 = \" value1 \" FIELD2 = \" value2\" \\>

为什么结果字符串中会显示\?这样的事情在C ++中是如此直接,在C#中让我疯狂,我缺少什么?b $ b缺少什么?

Brett ++



结束元素的好点修复了谢谢..但是我认为

编译器应该翻译\到字节代码0x0022 ..我看到

结果在几个地方,比如调试时的命令窗口,

工具提示当鼠标悬停在变量和值中插入

栏?我已经在C ++中完成了这个完全相同的过程......它确实很奇怪..但是我会尝试将它输出到控制台,看看会发生什么......

问题即使它显示正确,我仍然需要它存储

数据库中没有\ ..表格插入已经有

数千其他记录在其中由其他应用程序正确插入

并要求正确形成的XML ..


Brett ++

于9/21/04 12 :晚上19点,文章

_v******************@newssvr19.news.prodigy.com,Drebin

< th ******* @ hotmail.com>写道:

首先,自动关闭XML标签应该是< myelement /> *不*< myelement
\> - 这可能不会让你现在堵塞,但很可能会在以后发生!!

至于其他的事情,很可能就是你所看到的,是怎样的各种其他程序/ libraries / SQL存储这些特殊字符。
以下是正确的语法来做你想要的:

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

如果你要打印它,它会导致:

< row value =" data is here" />

尝试将其输出到屏幕或您知道将呈现特殊字符的某个位置。只是一个想法... hth

" Brett Hofer" < BH **** @ centermarktech.com>在消息中写道
新闻:BD75C9F4.345%bh **** @ centermarktech.com ...

我必须遗漏一些东西 - 我是一位资深的C ++程序员,现在正在使用
C#整体我喜欢这种语言,但发现了许多奇怪的变化......无论如何我在
后面编写代码。在这个C#方法中,我正在构建一个XML
字符串,以便插入到数据库中。该字符串应该导致:
< row FIELD1 =" value1" FIELD2 = QUOT;值2" \>

我正在使用字符串类型变量,我无法正确添加双引号我已经尝试了所有应该工作的东西


这样的

as:

string sXML;

sXML ="< row";

//我试过这个
sXML + =" FIELD1 = \" value1 \" " ;;
sXML + =" FIELD2 = \" value2 \" \\>" ;;

//这个
sXML + = @" FIELD1 ="" value1"" " ;;
sXML + = @" FIELD2 ="" value2"" \ \>" ;;

//这个
sXML + =" FIELD1 = \\\002222 = 00022" ;;
sXML + =" FIELD2 = \\ 00002 = 2> \ 0000 \\\ \\>>" ;;

在我检查这些情况的所有情况下,sXML字符串和/或插入到数据库中的值
结果in:
< row FIELD1 = \" value1 \" FIELD2 = \" value2\" \\>

为什么结果字符串中会显示\?这样的事情在C ++中如此简单,让我在C#中疯狂,我是什么


缺少



Brett ++





您也可以考虑使用XmlTextWriter来构造XML。

连接字符串有足够的开销,因为字符串对象是每次连接创建和销毁的时候。


" Brett Hofer" < BH **** @ centermarktech.com>在消息中写道

新闻:BD75C9F4.345%bh **** @ centermarktech.com ...

我必须遗漏一些东西 - 我是一位经验丰富的C ++程序员现在正在工作总的来说,我喜欢C#语言,但却发现了许多奇怪的变化......无论如何我在
后面编写代码。在这个C#方法中,我正在构建一个XML
字符串,以便插入到数据库中。该字符串应该导致:
< row FIELD1 =" value1" FIELD2 = QUOT;值2" \>

我正在使用字符串类型变量,我无法正确添加双引号我已经尝试了所有应该工作的东西
例如:

字符串sXML;

sXML ="< row" ;;

//我试过这个
sXML + =" FIELD1 = \" value1 \" " ;;
sXML + =" FIELD2 = \" value2 \" \\>" ;;

//这个
sXML + = @" FIELD1 ="" value1"" " ;;
sXML + = @" FIELD2 ="" value2"" \ \>" ;;

//这个
sXML + =" FIELD1 = \\\002222 = 00022" ;;
sXML + =" FIELD2 = \\ 00002 = 2> \ 0000 \\\ \\>>" ;;

在我检查这些情况的所有情况下,sXML字符串和/或插入到数据库中的值
结果in:
< row FIELD1 = \" value1 \" FIELD2 = \" value2\" \\>

为什么结果字符串中会显示\?这样的事情在C ++中是如此直接,在C#中让我疯狂,我缺少什么?b $ b缺少什么?

Brett ++



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
"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++



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++





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++



这篇关于C#中的双引号 - 简单的东西,但我觉得我很疯狂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆