C# String.Replace double quotes and Literals

前端 未结 4 768
孤街浪徒
孤街浪徒 2021-02-02 14:58

I\'m fairly new to c# so that\'s why I\'m asking this here.

I am consuming a web service that returns a long string of XML values. Because this is a string all the attri

相关标签:
4条回答
  • 2021-02-02 15:16

    the following statement in C#

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

    will actually store the value

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

    whereas

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

    have the value of

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

    for the second case, you need to replace the slash () by empty string as follow

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

    the result will be

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

    P.S.

    1. slash (\) is default escape character in C#
    2. to ignore slashes, use @ at the beginning of string
    3. if @ is used, the escape character is double quote (")
    0 讨论(0)
  • 2021-02-02 15:23

    There's no reason to use a Regular expression at all... that's a lot heavier than what you need.

    string xmlSample = "blah blah blah";
    
    xmlSample = xmlSample.Replace("\\\", "\"");
    
    0 讨论(0)
  • 2021-02-02 15:31

    If you are getting an XML string why not use XML instead strings?

    you will have access to all elements and attributes and it will be much easier and extremely fast if using the System.Xml namespace

    in your example you are getting this string:

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

    All you need to do is convert that string into a XML Document and use it, like:

    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/>";
    }
    

    in ASP.NET this will output to the Literal1 something like:

    item
    att1 = value
    att2 = value2
    

    once you have the element in a XmlElement, it is very easy to search and get the values and names for what's in that element.

    give it a try, I use it a lot when retrieving WebServices responses and when I store something in a XML file as settings for a small application for example.

    0 讨论(0)
  • 2021-02-02 15:38

    Both the string and the regex uses \ for escaping. The regex will see the character \ followed by ", and think it's a literal escape. Try this:

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

    You could also use raw strings (also known as veratim strings) in C#. They are prefixed with @, and all back-slashes are treated as normal characters. To include a quote in a raw string you need to double it.

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

    0 讨论(0)
提交回复
热议问题