简体   繁体   English

在C#中的String.Format中转义双引号

[英]Escaping double quotes within String.Format in C#

I am attempting to get the 'Id' property of the Parameter element to be enclosed in double quotes. 我正在尝试获取要用双引号引起来的Parameter元素的'Id'属性。 I first attempted to simply escape the quotations and this is the first thing I tried to achieve this: 我首先尝试简单地忽略引号,这是我尝试实现的第一件事:

buffer = String.Format("{0}" + "<Parameter Id=" + "{1}" + ">" + "{2}" + "</Parameter>", buffer, id, param);

With the above code I get this back, as you can see the escape characters are showing up, along with the quotation: 通过上面的代码,我将其弄回来,如您所见,出现了转义字符以及引号:

<Conquest><User>ArchElf</User><Token>0123456789012345678901234567890</Token><Command>validate</Command><Parameter Id=\"1\">Gemstone3</Parameter>

My second attempt is based on advised I received on IRC, a fellow advised that I may be able to use '"' to get my quotations, ala: 我的第二次尝试是基于我在IRC上收到的建议,另一个是建议我可以使用““”来获取报价,ala:

buffer = String.Format("{0}" + "<Parameter Id=" + "&quot;" + "{1}" + "&quot;" + ">" + "{2}" + "</Parameter>", buffer, id, param);

This method only yielded the literal '"' string in the end result: 此方法仅在最终结果中产生文字'“'字符串:

<Conquest><User>ArchElf</User><Token>0123456789012345678901234567890</Token><Command>validate</Command><Parameter Id=&quot;1&quot;>Gemstone3</Parameter>

In desperation I went ahead and just added the literal double quotes baked into the string. 无奈之下,我继续前进,只是在字符串中添加了文字双引号。

I did this because I read at This Codeproject Article that the only characters in a String.Format that I need to worry about escaping are curly braces and(surprise, surprise) this isn't even compile-able, WITH and WITHOUT the preceding @. 我这样做是因为我在本《代码项目》文章中读到,我需要担心转义的String.Format中唯一的字符是花括号,并且(惊讶,令人惊讶)这甚至都无法编译,使用WITH和WITHOUT前面的@ 。 Shouting at me a bunch of errors including: 向我大喊一堆错误,包括:

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement ; 只能将赋值,调用,递增,递减,等待和新对象表达式用作语句; Expected ) Expected ...and so on 预期)预期...等等

Any help on this matter would be greatly appreciated. 在此问题上的任何帮助将不胜感激。 I know this has got to be something trivial I am missing, the best kind of conundrums. 我知道这一定是我想念的琐碎小事,最好的难题。 :/ :/

Here is the entire BuildCommand method: 这是整个BuildCommand方法:

public string BuildCommand(string _command, string[] _parameters = null)
    {
        int id = 1;
        string buffer = String.Format("<Conquest><User>"+"{0}"+"</User><Token>"+"{1}"+"</Token><Command>"+"{2}"+"</Command>", _playerName, _token, _command);
        if (_parameters != null)
        {
            foreach (string param in _parameters)
            {
                if (param.Length < 1 || param == null)
                    break;
                buffer = String.Format("{0}" + "<Parameter Id=" + "{1}" + ">" + "{2}" + "</Parameter>", buffer, id, param);

                // buffer = String.Format(@"""{0}""<Parameter Id=""{1}"">""{2}""</Parameter>", buffer, id, param);
                id += 1;
            }
        }

You have to escape " with \\ : 您必须使用\\转义"

String.Format("\"{0}\"<Parameter Id=\"{1}\">\"{2}\"</Parameter>", buffer, id, param);

You could also use a verbatim string literal, then you have to use double quotes: 您还可以使用逐字字符串文字,然后必须使用双引号:

String.Format(@"""{0}""<Parameter Id=""{1}"">""{2}""</Parameter>", buffer, id, param);

You could do it the right way 你可以用正确的方式做

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //<Conquest><User>ArchElf</User><Token>0123456789012345678901234567890</Token><Command>validate</Command><Parameter Id=\"1\">Gemstone3</Parameter>

            string user = "ArchElf";
            string token = "0123456789012345678901234567890";
            string command = "validate";
            int id = 1;
            string value = "Gemstrone3";
            XElement conquest = new XElement("Conquest");

            conquest.Add(new XElement("User", user));

            conquest.Add(new XElement("Token", token));

            conquest.Add(new XElement("Command", command));

            XElement e_parameter = new XElement("Parameter", value);
            e_parameter.Add(new XAttribute("Id", id));
            conquest.Add(e_parameter);
        }
    }
}
​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM