Reputation: 1083
I want to take the text and some special characters between the xml tags.. My input file contains:
<line>public static void main(String[] args)</line>
<line>{</line>
<line> <>double <inline>result</inline> = Math.pow(2, 3);</line>
<line> . . .</line>
<line> </line> 'white space also comes
<line>}</line>
now i want the Regex to take text and the special characters between the tags <line>,<inline>..
Upvotes: 0
Views: 524
Reputation: 784
Parsing it as an XML will fail because of the <> before "double". I'd not use Regex for this either, but simple String.Replace will perform better or StringBuilder would be even better. This works fine for me:
string test = @"<line>public static void main(String[] args)</line>
<line>{</line>
<line> <>double <inline>result</inline> = Math.pow(2, 3);</line>
<line> . . .</line>
<line> </line> 'white space also comes
<line>}</line>
";
StringBuilder str = new StringBuilder(test);
new List<String>() { "<line>", "<inline>", "</line>", "</inline>" }.ForEach(token => str.Replace(token, String.Empty));
Console.WriteLine(str.ToString());
UPDATE:
with this it should work in .net 2.0 too:
StringBuilder str = new StringBuilder(test);
List<String> tokens = new List<String>();
tokens.Add("<line>");
tokens.Add("<inline>");
tokens.Add("</line>");
tokens.Add("</inline>");
foreach(String token in tokens)
{
str.Replace(token, String.Empty);
}
Upvotes: 3
Reputation: 217283
You can use LINQ to XML to parse XML:
var doc = XDocument.Parse(@"<lines>
<line>public static void main(String[] args)</line>
<line>{</line>
<line> <>double <inline>result</inline> = Math.pow(2, 3);</line>
<line> . . .</line>
<line> </line> 'white space also comes
<line>}</line>
</lines>", LoadOptions.PreserveWhitespace);
string result = doc.Root.Value;
Console.WriteLine(result);
Output:
public static void main(String[] args)
{
<>double result = Math.pow(2, 3);
. . .
'white space also comes
}
Upvotes: 0