ASP.NET C# 为 Froogle 编写 RSS feed

发布于 2024-08-27 03:27:50 字数 638 浏览 9 评论 0原文

我正在尝试使用 ASP.NET C# 创建 RSS 2.0 提要,并提供给 Froogle 的产品。

RSS Feed 应如下所示:

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

我使用 SyndicateFeed 和 SyndicatesItems 创建 Feed。但我在添加额外元素(例如 g:image_link)时遇到问题。

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

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

这可行,但如何将命名空间

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

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

谢谢

I'm trying to create a RSS 2.0 feed in ASP.NET C# with products to provide to Froogle.

The RSS feed should look like:

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

I'm using the SyndicationFeed and SyndicationsItems to create the feed. But I'm having trouble adding the extra elements like g:image_link.

I try the extra elements like;

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

This works, but how can I add the namespace

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

to the first RSS tag and use this for the extension elements?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-09-03 03:27:50

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

不过,我使用了 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>

I just wrote something like this last week, as a matter of fact. I didn't have much time, so it's not optimized or pretty.

I used an XDocument, though.

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;
}

(FYI: GoogleProduct is just a temporary mapper class I used)

It will generate a document along these lines

<?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>
幽梦紫曦~ 2024-09-03 03:27:50

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 上的一些完整示例:

如何:使用以下命令创建文档命名空间

XElements have great namespace support. Create your first element like this:

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

This will give you XML like this:

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

Each subsequent element should also use the same namespace. If you want to use namespace prefixes for your elements, it's a similar approach. You can check out some full examples on MSDN here:

How to: Create a Document with Namespaces

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文