What "efficiency" are you after? Expression efficiency or runtime efficiency? Here is a LINQ query that quickly finds an ad:
XmlDeclaration declaration = doc.ChildNodes
.OfType<XmlDeclaration>()
.FirstOrDefault();
I strongly suspect that it will be quite effective. Perhaps you could just check to see if there was a first child node XmlDeclaration... I don't think anything else might appear in front of it.
If you can use LINQ to XML, then it becomes even easier - you just use the property XDocument.Declaration.
source
share