static int GetLineNumber(string source, string tag)
{
// We'll manually walk through the source using an XmlTextReader
// so we can get the line number
using(var reader = new XmlTextReader(new StringReader(source))) {
if(reader.Read()) {
// Loop though all the top level children
while(reader.Read()) {
if(String.Compare(reader.Name, tag, StringComparison.OrdinalIgnoreCase) == 0)
return reader.LineNumber;
// If this node has children, ignore them all
if(reader.NodeType == XmlNodeType.Element && !reader.IsEmptyElement)
reader.Skip();
}
}
}
// If we can't find it, just use the first line
return 1;
}