Being a bit of a purist, I hate to see string constants propagated through my code:
var x = element.Attribute("Key");
I like them to be saved as one constant referenced, and I like the approach I first saw in Eric White XmlPowerTools :
public class MyNamespace { public static readonly XNamespace Namespace = "http://tempuri.org/schema"; public static readonly XName Key = Namespace + "Key"; }
and use:
var x = element.Attribute(MyNamespace.Key);
The beauty is that I can easily find all the links to these static fields using Visual Studio or Resharper, and I donβt have to go through every found instance of the βKeyβ line that I can imagine, it is used in many unrelated places.
So, I have an old project that I want to convert to an XName method, and you need to find all the occurrences where the string was used instead of XName . I believe that if I could stop the automatic conversion of the string to XName , the compiler would pick up all these occurrences for me.
I used regex in the search:
(Element|Attribute|Ancestor)s?\("
who took these constructs but then found:
if (element.Name == "key")
and thought what else would I miss.
The question is how to stop the automatic conversion of a string to XName or what other approach to find all occurrences of a string, which should be XName ?
source share