【问题标题】:C# String.Replace double quotes and LiteralsC# String.Replace 双引号和文字
【发布时间】:2008-11-12 22:17:21
【问题描述】:

我对 c# 还很陌生,所以我在这里问这个问题。

我正在使用一个返回一长串 XML 值的 Web 服务。因为这是一个字符串,所以所有属性都转义了双引号

string xmlSample = "<root><item att1=\"value\" att2=\"value2\" /></root>"

这是我的问题。我想做一个简单的string.replace。如果我在 PHP 中工作,我只需运行 strip_slashes()。

但是,我在 C# 中,我一生都无法弄清楚。我无法写出我的表达式来替换双引号 ("),因为它终止了字符串。如果我转义它,那么它的结果不正确。我做错了什么?

    string search = "\\\"";
    string replace = "\"";
    Regex rgx = new Regex(search);
    string strip = rgx.Replace(xmlSample, replace);

    //Actual Result  <root><item att1=value att2=value2 /></root>
    //Desired Result <root><item att1="value" att2="value2" /></root>

MizardX:要在原始字符串中包含引号,您需要将其加倍。

这是重要的信息,现在尝试这种方法......那里也没有运气 双引号在这里发生了一些事情。你们都建议的概念是可靠的,但这里的问题是处理双引号,看起来我需要做一些额外的研究来解决这个问题。如果有人想出什么,请发布答案。

string newC = xmlSample.Replace("\\\"", "\"");
//Result <root><item att=\"value\" att2=\"value2\" /></root> 

string newC = xmlSample.Replace("\"", "'");
//Result newC   "<root><item att='value' att2='value2' /></root>"

【问题讨论】:

  • 绝对不要在这里使用正则表达式;只需使用替换功能!
  • 查看最后一个答案 - 你已经完成了你想要的,只是你看待它的方式让你一团糟。
  • 我同意,我什至看不到这里的问题。斜杠不是字符串中的真实字符,它们是转义标记!
  • 谢谢大家,我只需要关于文字等的解释。我标记了我的答案。它工作正常,正如我所期望的那样,我正在用正确的字符串替换不正确的字符串...grrrr。

标签: c# xml


【解决方案1】:

C#中的以下语句

string xmlSample = "<root><item att1=\"value\" att2=\"value2\" /></root>"

实际上会存储值

<root><item att1="value" att2="value2" /></root>

string xmlSample = @"<root><item att1=\""value\"" att2=\""value2\"" /></root>";

有值

<root><item att1=\"value\" att2=\"value2\" /></root>

对于第二种情况,您需要将斜杠()替换为空字符串,如下所示

string test = xmlSample.Replace(@"\", string.Empty);

结果将是

<root><item att1="value" att2="value2" /></root>

附:

  1. 斜线 (\) 是 C# 中的默认转义字符
  2. 要忽略斜线,在字符串的开头使用@
  3. 如果使用@,则转义字符为双引号(")

【讨论】:

  • 我会更正确地说verbatim literal string 没有转义字符,特殊字符"{} 是通过加倍来转义的。
【解决方案2】:

根本没有理由使用正则表达式...这比您需要的要重得多。

string xmlSample = "blah blah blah";

xmlSample = xmlSample.Replace("\\\", "\"");

【讨论】:

    【解决方案3】:

    字符串和正则表达式都使用\ 进行转义。正则表达式将看到字符\ 后跟",并认为这是文字转义。试试这个:

    Regex rgx = new Regex("\\\\\"");
    string strip = rgx.Replace(xmlSample, "\"");
    

    您还可以在 C# 中使用原始字符串(也称为验证字符串)。它们以@ 为前缀,所有反斜杠都被视为普通字符。要在原始字符串中包含引号,您需要将其加倍。

    Regex rgx = new Regex(@"\""")
    string strip = rgx.Replace(xmlSample, @"""");

    【讨论】:

      【解决方案4】:

      如果你得到一个 XML 字符串,为什么不使用 XML 代替字符串?

      您将可以访问所有元素和属性,如果使用 System.Xml 命名空间,这将变得更加容易和快速

      在你的例子中你得到这个字符串:

      string xmlSample = "<root><item att1=\"value\" att2=\"value2\" /></root>";
      

      您需要做的就是将该字符串转换为 XML 文档并使用它,例如:

      System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
      xml.LoadXml(xmlSample);
      
      System.Xml.XmlElement _root = xml.DocumentElement;
      
      foreach (System.Xml.XmlNode _node in _root)
      {
          Literal1.Text = "<hr/>" + _node.Name + "<br/>";
          for (int iAtt = 0; iAtt < _node.Attributes.Count; iAtt++)
              Literal1.Text += _node.Attributes[iAtt].Name + " = " + _node.Attributes[iAtt].Value + "<br/>";
      }
      

      在 ASP.NET 中,这将输出到 Literal1,如下所示:

      item
      att1 = value
      att2 = value2
      

      一旦您在 XmlElement 中拥有该元素,就很容易搜索并获取该元素中内容的值和名称。

      试一试吧,我在检索 WebServices 响应以及将某些内容存储在 XML 文件中作为小型应用程序的设置时经常使用它。

      【讨论】:

        猜你喜欢
        • 2011-09-21
        • 2012-05-23
        • 1970-01-01
        • 2020-05-23
        • 2022-06-16
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多