XName.Get(): The Function I May Never Call Again

November 10th, 2008 by jason Leave a reply »

I love the new System.Xml.Linq namespace. Building and consuming XML is so much easier with the new XDocument, XElement, and other XLinq classes. But one thing that has bugged me was the need to generate a tag name with XName.Get(“TagName”). For example:

XElement element = new XElement(XName.Get(“TagName”));

This statement seems to go against the spirit of the XLinq classes: simplicity. Why do I have to use the XName class just to name my tag? Well, as it turns out this is more than is needed. On a wild hunch I ripped open XName in Reflector today found an implicit operator to cast from string to XName. Damn! So all this time I could have been writing:

XElement element = new XElement(“TagName”);

And the XName implicit operator would have cast it to XName for me automatically.

Advertisement

Leave a Reply

You must be logged in to post a comment.