Using xname in Linq-to-xml

c#, linq, linq-to-xml

Solution

There is a static method on XName called Get that allows you to create an XName. However, there is also an implicit cast from string to XName, so if you just enter a string, it should be able to conver to XName and work without problems

Problem

I am writing some code to generate an opml file from a list of rss feeds (parsed) on my site. The user will select checkboxes from a datagrid of rss feeds on my site, and when pressing a button, the heavy lifting will happen. Anyway, I have code like this: ``` foreach (var v in list) { XName xname; doc.Element("channel").Add( new XElement("title", v.Name), new XElement("description", "First Article Description"), new XElement("pubDate", DateTime.Now.ToUniversalTime()), new XElement("guid", Guid.NewGuid())); } ``` list is a collection of feed objects (e.g. hanselman rss feed, codinghorror rss feed, etc). The datagrid will have a checkbox and pressing the button below this grid, the code above will execute (I have also got the code for the xml declarations etc). When I use the Element(...) method, I need to provide XName. This has an internal constructor which I cannot use. How can I pass this parameter in?

Original source