how to use double Quotes correctly?
This form works in VB .NET
sendMsg = "<CStatus timestamp=""0"" " & _
"type=""login"" " & _
"cid = """ & cID & """ " & _
"key=""" & loginKey & """ />"
But i can't get it work in C#
sendMsg = "<CStatus timestamp=\"0\"
type=\"login\"
cid=\"" + cID + "\"
key=\"" + loginKey + "\" />";
it does not give the same effect
I want this as an output:
<CStatus timestamp="0" type="login" cid="var_cid" key="var_key"/>;
is there any stringXml command in C# or another way to use double quotes in string?
Thank You!
Solved it with开发者_开发问答
XmlTextWriter
Thanks to you all!
Embedded newlines are not supported for string literals in C#. Try:
sendMsg = "<CStatus timestamp=\"0\" " +
"type=\"login\" " +
"cid=\"" + cID + "\" " +
"key=\"" + loginKey + "\" />";
Your problem does not seem to be related to your use of quotes.
Another option is to use the XElement and XAttribute classes which will work both in VB and C#:
string cID = "blah";
string loginKey = "foo";
var xml = new XElement("CStatus",
new XAttribute("timestamp", "0"),
new XAttribute("type", "login"),
new XAttribute("cid", cID),
new XAttribute("key", loginKey)
);
string sendMsg = xml.ToString();
When using quotes in C#, you would escape them with the backslash \"
, as you have tried.
However, when you are splitting your string across multiple lines, try the alternate path of marking the string with @
and then double the quotes for proper escaping.
string theString = @"<CStatus timespamp=""0""
type=""login""
/>"; // fill in the rest
String.Format(@"<CStatus timestamp=""0""
type=""login""
cid=""{0}""
key=""{1}"" />", cID, loginKey);
There's a couple of ways you can do this.
In the current way you're trying to do it, it won't work. You're trying to put a newline into a constant string. You'd have to rewrite it as:
sendMsg = "<CStatus timestamp=\"0\" " +
"type=\"login\" " +
"cid=\"" + cID + "\" " +
"key=\"" + loginKey + "\" />";
Notice the extra +'s at the end of every line and how each line is its own string constant.
I think it might be easier to just use String.Format in this case:
sendMsg = String.Format("<CStatus timestamp=\"0\" type=\"login\" cid=\"{0}\" key=\"{1}\" />",
cID, loginKey);
Quotes are escaped like this in c#: \"
for example, timestamp="0"
with double quotes should be timestamp=\"\"0\"\"
Try using double quotes as you do in vb, but start your string definition whith @
string myString = @"<xml attribute=""my attribute""/>";
You can check this link to see other methods to scape double quotes: http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx
sendMsg = "<CStatus timestamp=\"0\" " +
"type=\"login\" " +
"cid=\"" + cID + "\" +
"key=\"" + loginKey + "\" />";
sendMsg = @"<CStatus timestamp=""0"" type=""login"" cid=""" + cID + """ key=""" + loginKey + """ />";
As Greg Hewgill says, you have newlines in your strings in the c# version.
You really should use String.Format here, but to answer your question, your code should look like:
sendMsg = "<CStatus timestamp=\"0\" +
"type=\"login\"" +
"cid=\"" + cID + "\"" +
"key=\"" + loginKey + "\" />";
As a String.Format call, it would be:
sendMsg = String.Format(@"<CStatus timestamp=""{0}""
type=""{1}""
cid=""{2}""
key=""{3}""/>", 0, login, cID, loginKey );
精彩评论