2010-03-30 148 views
0

我正在尝试在ASP.NET C#中创建RSS提要,并提供给Froogle的产品。ASP.NET C#编写Froogle的RSS订阅源

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标签和使用这个扩展元素?

谢谢

回答

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

(FYI: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> 
+0

感谢您的信息。我希望有可能使用SyndicationItem等,但我会尝试你的方法。 – Peter 2010-03-30 20:44:40

+0

通过一切手段,探索!就像我说的那样,我受到了时间的压力,而且我对Linq-to-XML很满意,所以这对我来说是一条自然的道路。 – 2010-03-30 20:46:27

+0

我一直在探索找到它,但似乎不可能使用SyndicateFeed将RSS名称空间添加到RSS标签:( – Peter 2010-03-30 21:00:09

1

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

+0

谷歌需要的文件(大部分)有克元素:不幸的是,前缀。它想要g:image_link,g:价格等。 – 2010-03-30 20:26:08

+0

没错。看看链接,它显示了如何为元素添加前缀。您只需将其创建为新的XElement(名称,前缀,内容),其中前缀是XElementAttribute。 – womp 2010-03-30 20:34:35

+0

是的,我通过将它包含在“rss”行中的一个属性中来实现它。 '新的XAttribute(XNamespace.Xmlns +“g”,xnamespace)'。随后的XElements只需要命名为名称空间+实际的元素名称。 (请参阅我的回答。) – 2010-03-30 20:40:58