using System.Collections.Generic;
using System.Text.RegularExpressions;
public static bool checkXml(String input)
Stack<string> startStack = new Stack<string>();
Regex regx = new Regex("<[^<>]+>");
foreach (Match match in regx.Matches(input))
if(match.Value.StartsWith("</"))
if(startStack.Count>0 && startStack.Peek().Equals(match.Value.Replace("</","<")))
Console.WriteLine("stack pop: "+String.Join(" ", startStack));
startStack.Push(match.Value);
Console.WriteLine("stack push "+String.Join(" ", startStack));
return startStack.Count==0;
public static void Main()
string input1 = "<Design><Code>Hello World</Code></Design>";
Console.WriteLine("input: "+input1);
Console.WriteLine(checkXml(input1));
string input2 = "<Design><Code>Hello World</Code></Design><People>";
Console.WriteLine("input: "+input2);
Console.WriteLine(checkXml(input2));
string input3 = "<People><Design><Code>Hello World</People></Code></Design>";
Console.WriteLine("input: "+input3);
Console.WriteLine(checkXml(input3));
string input4 = "<a><b>bvalue</b><c>cvalue</c><d><e>evalue</e><f>fvalue</f></d></a>";
Console.WriteLine("input "+input4);
Console.WriteLine(checkXml(input4));