<p>I'm writing a lightweight XML editor, and in cases where the user's input is not well formed, I would like to indicate to the user where the problem is, or at least where the first problem is.  Does anyone know of an existing algorithm for this?  If looking at code helps, if I could fill in the FindIndexOfInvalidXml method (or something like it), this would answer my question.</p>

<pre><code>using System;

namespace TempConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "&lt;?xml version=\"1.0\"?&gt;&lt;tag1&gt;&lt;tag2&gt;Some text.&lt;/taagg2&gt;&lt;/tag1&gt;";
            int index = FindIndexOfInvalidXml(text);
            Console.WriteLine(index);
        }

        private static int FindIndexOfInvalidXml(string theString)
        {
            int index = -1;

            //Some logic

            return index;
        }
    }
}
</code></pre>

Unless this is an academic exercise, I think that writing your own XML parser is probably not the best way to go about this.  I would probably check out the
XmlDocument class
within the System.Xml namespace and try/catch
exceptions
for the Load() or LoadXml() methods.  The exception's message property should contain info on where the error occurred (row/col numbers) and I suspect it'd be easier to use a regular expression to extract those error messages and the related positional info.