简体   繁体   中英

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 (eg 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?

您还可以将名称空间括在花括号中:

XName name = "{http://schemas.xyz.com/namespaceUri}tagName";

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

创建一个XNamespace对象并使用加法运算符:

XName name = (XNamespace)"http://schemas.xyz.com/namespaceUri" + "tagName";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM