Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using xname in Linq-to-xml

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?

like image 768
GurdeepS Avatar asked Aug 22 '09 16:08

GurdeepS


3 Answers

You can also enclose a namespace in curly braces:

XName name = "{http://schemas.xyz.com/namespaceUri}tagName";
like image 174
Joe Chung Avatar answered Oct 04 '22 09:10

Joe Chung


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

like image 22
LorenVS Avatar answered Oct 04 '22 11:10

LorenVS


Create a XNamespace object and use the addition operator:

XName name = (XNamespace)"http://schemas.xyz.com/namespaceUri" + "tagName";
like image 38
mmx Avatar answered Oct 04 '22 11:10

mmx