3

I am trying to replace within a string

<?xml version="1.0" encoding="UTF-8"?>
<response success="true">
<output><![CDATA[

And

]]></output>
</response>

with nothing. The problem I am running into is the characters <> and " characters are interacting within the replace. Meaning, it's not reading those lines as a full string all together as one but breaking the string when it comes to a <> or ". Here is what I have but I know this isn't right:

String responseString = reader.ReadToEnd();
            responseString.Replace(@"<<?xml version=""1.0"" encoding=""UTF-8""?><response success=""true""><output><![CDATA[[", "");
            responseString.Replace(@"]]\></output\></response\>", "");  

What would be the correct code to get the replace to see these lines as just a string?

  • 2
    Have you considered XML entities like `啤`? This could be something else that you need to handle. You may want to [use XML parsing capabilities](http://stackoverflow.com/questions/12490637/xml-parsing-reading-cdata) instead. – zneak May 28 '15 at 17:45
  • Why don't you use regex? https://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx – tdbeckett May 28 '15 at 17:45
  • @user3444160 This post explains why http://stackoverflow.com/a/1732454/668272 – Bas May 28 '15 at 17:54

2 Answers2

4

A string will never change. The Replace method works as follows:

string x = "AAA";
string y = x.Replace("A", "B");
//x == "AAA", y == "BBB"

However, the real problem is how you handle the XML response data.


You should reconsider your approach of handling incoming XML by string replacement. Just get the CDATA content using the standard XML library. It's as easy as this:

using System.Xml.Linq;
...
XDocument doc = XDocument.Load(reader);
var responseString = doc.Descendants("output").First().Value;

The CDATA will already be removed. This tutorial will teach more about working with XML documents in C#.

Bas
  • 26,772
  • 8
  • 53
  • 86
0

Given your document structure, you could simply say something like this:

string response = @"<?xml version=""1.0"" encoding=""UTF-8""?>"
                + @"<response success=""true"">"
                + @"  <output><![CDATA["
                + @"The output is some arbitrary text and it may be found here."
                + "]]></output>"
                + "</response>"
                ;
XmlDocument document = new XmlDocument() ;
document.LoadXml( response ) ;

bool success ;
bool.TryParse( document.DocumentElement.GetAttribute("success"), out success)  ;

string content = document.DocumentElement.InnerText ;

Console.WriteLine( "The response indicated {0}." , success ? "success" : "failure" ) ;
Console.WriteLine( "response content: {0}" , content ) ;

And see the expected results on the console:

The response indicated success.
response content: The output is some arbitrary text and it may be found here.

If your XML document is a wee bit more complex, you can easily select the desired node(s) using an XPath query, thus:

string content = document.SelectSingleNode( @"/response/output" ).InnerText;
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135