i have the string which is a part of an xml.
a<b>b</b>c<i>d</i>e<b>f</b>g
the problem is that i want to extract from the string the parts that are not inside any tags. so i need to extract the string"aceg" from this string and leave the characters "bdf" how can this be done?
Edit: this was a part of an xml let asume its
<div>a<b>b</b>c<i>d</i>e<b>f</b>g</div>
now its a valid xml :)
The following regular expression will remove all tags from the string:
Regex.Replace("a<b>b</b>c<i>d</i>e<b>f</b>g", "<[^>]+>", string.Empty);
That string is not valid XML.
However, assuming you had a valid XML string, then you could do something like this:
class Program
{
static void Main(string[] args)
{
string contents = string.Empty;
XmlDocument document = new XmlDocument();
document.LoadXml("<outer>a<b>b</b>c<i>d</i>e<b>f</b>g</outer>");
foreach(XmlNode child in document.DocumentElement.ChildNodes)
{
if (child.NodeType == XmlNodeType.Element)
{
contents += child.InnerText;
}
}
Console.WriteLine(contents);
Console.ReadKey();
}
}
This will print out the string "bdf"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With