2010-03-30 146 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