As it turns out, this is
very easy to do with the node structure. I don't normally do this, but it's probably more worthwhile in this case if I just show you the code than if I try to lead you through it.
- string xmlRaw = "<text display-inline=\"yes\">This<b>is</b>only<ref>a</ref><i>sample</i></text>";
-
-
XmlDocument xmlDoc = new XmlDocument();
-
xmlDoc.LoadXml(xmlRaw);
-
-
List<string> items = new List<string>();
-
-
foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
-
{
-
switch (node.NodeType)
-
{
-
case XmlNodeType.Text:
-
items.Add(node.Value);
-
break;
-
case XmlNodeType.Element:
-
items.Add(string.Format("<{0}>{1}</{0}>", node.Name, node.InnerText));
-
break;
-
}
-
}
-
-
for (int i = 0; i < items.Count; i++)
-
{
-
Console.WriteLine(string.Format("items[{0}] = {1}", i.ToString(), items[i]));
-
}
This is obviously making liberal use of assumptions, but you can quite easily detect if a node is a text node, then do this with its children to get the same result.
Basically, when it gets read into an XmlDocument object, a hierarchical structure is generated and the appropriate node types are setup for you. It's a simple matter of going through the children of a text node and seeing if they are text, or an element, building the appropriate string, and adding it to an array (list, in this case).
The output of this code is...
items[0] = This
items[1] = <b>is</b>
items[2] = only
items[3] = <ref>a</ref>
items[4] = <i>sample</i>