【问题标题】:ASP.NET C# Write RSS feed for FroogleASP.NET C# 为 Froogle 编写 RSS 提要
【发布时间】:2011-02-02 15:40:02
【问题描述】:

我正在尝试在 ASP.NET C# 中创建一个 RSS 2.0 提要,其中包含要提供给 Froogle 的产品。

RSS 提要应如下所示:

http://www.google.com/support/merchants/bin/answer.py?answer=160589&hl=en

我正在使用 SyndicationFeed 和 SyndicationsItems 创建提要。但我无法添加额外的元素,如 g:image_link。

我尝试了额外的元素,例如;

syndicationItem.ElementExtensions.Add(new XElement("image_link", product.ImageLink).CreateReader());

这可行,但我如何添加命名空间

xmlns:g="http://base.google.com/ns/1.0"

到第一个 RSS 标记并将其用于扩展元素?

谢谢

【问题讨论】:

    标签: c# asp.net rss syndication


    【解决方案1】:

    事实上,我上周刚写过类似的东西。我没有太多时间,所以它没有优化或漂亮。

    不过,我使用的是 XDocument。

    static XDocument GetXDocument(List<GoogleProduct> googleProducts)
    {
        XNamespace gns = "http://base.google.com/ns/1.0";
    
        XDocument document = new XDocument(
            new XElement("rss",
                new XAttribute("version", "2.0"),
                new XAttribute(XNamespace.Xmlns + "g", gns),
                new XElement("channel",
                    new XElement("title", "X Company Feed"),
                    new XElement("description", "X Description"),
                    new XElement("link", "http://www.somecompany.com/"),
                    from googleProduct in googleProducts
                    select new XElement("item",
                        new XElement("title", googleProduct.Title),
                        new XElement(gns + "brand", googleProduct.ProductRecommendedAttributes.Brand),
                        new XElement(gns + "manufacturer", googleProduct.ProductRecommendedAttributes.Manufacturer),
                        new XElement(gns + "condition", googleProduct.Condition),
                        new XElement("description", googleProduct.Description),
                        new XElement(gns + "id", googleProduct.ID),
                        from img in googleProduct.ProductRecommendedAttributes.ImageLinks
                        select new XElement(gns + "image_link", img),
                        new XElement("link", googleProduct.Link),
                        new XElement(gns + "price", googleProduct.Price.ToString("0.00")),
                        new XElement(gns + "product_type", googleProduct.ProductRecommendedAttributes.ProductType),
                        from pmt in googleProduct.ProductOptionalAttributes.PaymentAccepteds
                        select new XElement(gns + "payment_accepted", pmt)))));
    
        //
        return document;
    }
    

    (仅供参考:GoogleProduct 只是我使用的一个临时映射器类)

    它将按照这些思路生成一个文档

    <?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
      <channel>
        <title>Blah Data Feed</title>
        <description>Stuff from Blah</description>
        <link>http://www.blah.com/shopping</link>
        <item>
          <title>Blah</title>
          <g:brand>Blah</g:brand>
          <g:manufacturer>Blah</g:manufacturer>
          <g:condition>New</g:condition>
          <description>blah blah</description>
          <g:id>268</g:id>
          <g:image_link>http://www.blah.com/shopping/images/PRODUCT/medium/268.jpg</g:image_link>
          <link>http://www.blah.com/</link>
          <g:price>1747.00</g:price>
          <g:product_type>Blah Blah</g:product_type>
          <g:payment_accepted>Cash</g:payment_accepted>
          <g:payment_accepted>Check</g:payment_accepted>
          <g:payment_accepted>Visa</g:payment_accepted>
          <g:payment_accepted>Mastercard</g:payment_accepted>
        </item>
        <item>
          <title>Blah</title>
          <g:brand>Blah</g:brand>
          <g:manufacturer>Blah</g:manufacturer>
          <g:condition>New</g:condition>
          <description>blah blah</description>
          <g:id>269</g:id>
          <g:image_link>http://www.blah.com/shopping/images/PRODUCT/medium/269.jpg</g:image_link>
          <link>http://www.blah.com/</link>
          <g:price>1103.00</g:price>
          <g:product_type>blah blah</g:product_type>
          <g:payment_accepted>Cash</g:payment_accepted>
          <g:payment_accepted>Check</g:payment_accepted>
          <g:payment_accepted>Visa</g:payment_accepted>
          <g:payment_accepted>Mastercard</g:payment_accepted>
        </item>
      </channel>
    </rss>
    

    【讨论】:

    • 感谢您的信息。我希望可以使用 SyndicationItem 等,但我会尝试你的方法。
    • 无论如何,探索!就像我说的,我有时间压力,我最喜欢使用 Linq-to-XML,所以这对我来说是一条自然的道路。
    • 我整天都在寻找它,但似乎不可能使用 SyndicateFeed 为 RSS 标签添加命名空间:(
    【解决方案2】:

    XElements 有很好的命名空间支持。像这样创建你的第一个元素:

    XNamespace aw = "http://base.google.com/ns/1.0";
    XElement root = new XElement(aw + "image_link", product.ImageLink);
    

    这会给你这样的 XML:

    <image_link xmlns="http://base.google.com/ns/1.0">
    </image_link>
    

    每个后续元素也应该使用相同的命名空间。如果你想为你的元素使用命名空间前缀,这是一种类似的方法。您可以在此处查看 MSDN 上的一些完整示例:

    How to: Create a Document with Namespaces

    【讨论】:

    • 不幸的是,Google 需要(大部分)元素具有 g: 前缀。它需要 g:image_link、g:price 等。
    • 是的,没关系。查看链接,它显示了如何为元素添加前缀。您只需将其创建为 new XElement(name, prefix, content),其中 prefix 是一个 XElementAttribute。
    • 是的,我通过将它包含在“rss”行的属性中来实现它。 new XAttribute(XNamespace.Xmlns + "g", xnamespace)。随后的 XElements 只需命名为命名空间 + 实际元素名称。 (见我的回答。)
    猜你喜欢
    • 2010-09-08
    • 2010-09-11
    • 1970-01-01
    • 2015-03-30
    • 2010-10-29
    • 2013-02-22
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    相关资源
    最近更新 更多