I prefer to use
XDocument class[
^] which is very "flexible" when there's a need to implement custom search method. See:
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
XDocument xdoc = XDocument.Load(XmlReader.Create("fullfilename.xml", settings));
var cons = xdoc.Descendants("xref")
.GroupBy(x=>x.Parent)
.Select(grp=> new
{
Parent = grp.Key,
ConsecutiveNodes = grp.Select((n, i)=> new
{
Index = i+1,
Node = n
}),
Count = grp.Count()
})
.ToList();
Console.WriteLine("3 or more consecutive nodes:");
foreach(var o in cons)
{
if (o.Count>2)
{
Console.WriteLine("{0}", new string('=', 30));
Console.WriteLine("Found in: {0} ... {1}", o.Parent.ToString().Substring(0,15), o.Parent.ToString().Substring(o.Parent.ToString().Length-15,15));
Console.WriteLine("{0}", new string('-', 50));
foreach (var c in o.ConsecutiveNodes)
{
Console.WriteLine("Original rid value [{0}] will be replaced with [{1}]", c.Node.Attribute("rid").Value, c.Index);
c.Node.Attribute("rid").Value = c.Index.ToString();
}
}
}
Above code displays:
3 or more consecutive nodes:
==============================
Found in: <p>In this stud ... 15]</xref>.</p>
--------------------------------------------------
Original rid value [ref2] will be replaced with [1]
Original rid value [ref3] will be replaced with [2]
Original rid value [ref4] will be replaced with [3]
Original rid value [ref20] will be replaced with [4]
Original rid value [ref3] will be replaced with [5]
Original rid value [ref15] will be replaced with [6]
==============================
Found in: <p>The measurin ... cattering..</p>
--------------------------------------------------
Original rid value [ref11] will be replaced with [1]
Original rid value [ref12] will be replaced with [2]
Original rid value [ref13] will be replaced with [3]
Original rid value [ref4] will be replaced with [4]
Original rid value [T2] will be replaced with [5]
For further information, please see:
XDocument.Load Method (XmlReader) (System.Xml.Linq)[
^]
XmlReaderSettings.DtdProcessing Property (System.Xml)[
^]
Feel free to change code to your needs. Good luck!