I have an XML file and I need to extract data out of it.This task would be trivial if I only could use Xdocument, but the whole point of exercise is to create own parser using regex. The XML looks similar to below:
<A>
<B>
<C>ASD</C>
</B>
<B>
<C>ZXC</C>
</B>
</A>
I Came up with an idea that I can divide input to both closing and opening tag and their content.
string acquiredFile = myStringBuilder.ToString();
string regexPattern = "(?<open><[A-z0-9]{1,}>)(?<content>.*)(?<close></[A-z0-9]{1,}>)";
Regex rx = new Regex(regexPattern, RegexOptions.Singleline);
foreach (Match match in Regex.Matches(acquiredFile, regexPattern, RegexOptions.Singleline))
{
Console.WriteLine(match.Groups["open"].Value);
Console.WriteLine(match.Groups["content"].Value);
Console.WriteLine(match.Groups["close"].Value);
}
I need to wrap it up in loop. Above extraction solution works only for single nested element in XML document such as:
<A>
<B>
<C>ASD</C>
</B>
</A>
Could you please help me how to expand this code to get it to work with multiple nested elements.