0

I have a string value in the column as in a tab demilited text file and want to extract the value within it 'John Smith' and store it in as a string value.I tried using string.Replace but not sure of the I have the correct implementation for it.

     string cdatastring = "<![CDATA[John Smith]]>";
     string expected = "John Smith"; 

How do I implement it using C#/.NET methods?

Darshak
  • 31
  • 5

1 Answers1

0

You can use the LINQ to XML method XElement.Parse() to parse any well-formed XML document, however your CDATA string <![CDATA[John Smith]]> is not a well-formed XML document, it's just a document fragment, since it lacks a root element. To convert it to well-formed XML, you can surround it by a synthetic root element, parse that, and extract the value like so:

var value = XElement.Parse("<a>" + cdatastring + "</a>").Value;

If your cdatastring is longer than 42,000 characters or so and thus the string with the added root element would go on the large object heap, you could grab ChainedTextReader and public static TextReader Extensions.Concat(this TextReader first, TextReader second) from the answer to How to string multiple TextReaders together? by Rex M and create the following extension method:

public static class XmlExtensions
{
    public static XElement ParseFragment(string xmlFragment)
    {
        using (var reader = new StringReader("<a>").Concat(new StringReader(xmlFragment)).Concat(new StringReader("</a>")))
        {
            return XElement.Load(reader);
        }
    }
}

Then extract your CDATA value like so:

var value = XmlExtensions.ParseFragment(cdatastring).Value;

Sample working .Net fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340