strip

  • Thread starter Thread starter John A Grandy
  • Start date Start date
Let's say you are storing lots of URLs in XML files. So, for those with
non-XML-compliant characters you use the <![CDATA]]> construct ...

<Element1><![CDATA[http://overthetop.com?search=too much&sort=populatity]]></Element1>

Does .NET provide an intrinsic function that will strip the URL out of the
<![CDATA]]> construct ?

What do you mean by "strip" here?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
I am going to assume your are talking about the & character in the URL.
The & character is only used as an escape character within xml and
needs to be replaced with &amp; to be legal.

Your sample URL becomes
<Element1>http://overthetop.com?search=too much&amp;sort=populatity</Element1>,
which is valid XML and cam be used as is for hyperlinks.

Here is some sample code to replace & in a URL with &amp;

public static void ReplaceRegex()
{
// Regex search and replace
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"&", options);
string input =
@"http://overthetop.com?search=too much&sort=populatity";
string replacement = @"&amp;";
string result = regex.Replace(input, replacement);

// TODO: Do something with result
System.Windows.Forms.MessageBox.Show(result, "Replace");
}
 
Yeah, I know, I tried that solution. The problem is that other people keep
on updating the config files with new URLs and not replacing "&" with
"&amp;" ..

Anyway, I figured out the answer ;

use XmlNode.InnerText instead of XmlNode.InnerXml
 
Back
Top