CSharp · Console
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
//https://stackoverflow.com/questions/51520869/get-value-within-the-cdata-within-tab-delimited-text-file-using-c-sharp
public class TestClass
{
public static void Test()
{
string cdatastring = "<![CDATA[John Smith]]>";
string expected = "John Smith";
TestSimple(cdatastring, expected);
TestReaders(cdatastring, expected);
}
static void TestSimple(string cdatastring, string expected)
{
var value = XElement.Parse("<a>" + cdatastring + "</a>").Value;
Console.WriteLine(value); // Prints John Smith
Assert.IsTrue(value == expected);
}
static void TestReaders(string cdatastring, string expected)
{
var value = XmlExtensions.ParseFragment(cdatastring).Value;
Console.WriteLine(value); // Prints John Smith
Assert.IsTrue(value == expected);
}
}
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);
}
}
}
public static class StringExtensions
{
// Taken from https://stackoverflow.com/questions/2925652/how-to-string-multiple-textreaders-together/2925722#2925722
// by Rex M
public static TextReader Concat(this TextReader first, TextReader second)
{
return new ChainedTextReader(first, second);
}
private class ChainedTextReader : TextReader
{
private TextReader first;
private TextReader second;
private bool readFirst = true;
public ChainedTextReader(TextReader first, TextReader second)
{
this.first = first;
this.second = second;
}
public override int Peek()
{
if (readFirst)
{
return first.Peek();
}
else
{
return second.Peek();
}
}
public override int Read()
{
if (readFirst)
{
int value = first.Read();
if (value == -1)
{
readFirst = false;
}
else
{
return value;
}
}
return second.Read();
}
public override void Close()
{
first.Close();
second.Close();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
first.Dispose();
second.Dispose();
}
}
}
}
public class AssertionFailedException : System.Exception
{
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
}
public static class Assert
{
public static void IsTrue(bool value)
{
IsTrue(value, "failed");
}
public static void IsTrue(bool value, string message)
{
if (value == false)
throw new AssertionFailedException(message ?? "failed");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Uncaught exception: ");
Console.WriteLine(ex);
throw;
}
}
}