2010-03-30 5 views
0

Froogle에 제공 할 제품으로 ASP.NET C#에서 RSS 2.0 피드를 만들려고합니다. 내가 피드를 생성하기 위해 SyndicationFeed 및 SyndicationsItems을 사용하고ASP.NET C# Froogle 용 RSS 피드 작성

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

: 같은

RSS 피드가 보일 것입니다. 그러나 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; 
} 

: 그것은 g 가지고 (대부분의) 요소를

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

+0

꼭 탐험 해보세요! 내가 말했듯이, 나는 시간 압박감을 느꼈고 Linq-to-XML에 가장 익숙해 져서 자연스러운 길이었습니다. –

+0

나는 그것을 찾기 위해 하루 종일 탐색 해왔다. 그러나 SyndicateFeed를 사용하여 RSS 태그에 네임 스페이스를 추가하는 것은 불가능 해 보인다. ( – Peter

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

구글이 필요로 이러한 라인을 따라 문서를 생성합니다

(참고로 GoogleProduct 내가 사용하는 단지 임시 매퍼 클래스입니다) : 접두사, 불행히도. 그것은 g를 원합니다 : image_link, g : 가격 등 –

+0

그래, 괜찮아. 링크를 살펴보면 요소에 접두어를 추가하는 방법을 보여줍니다. 새로운 XElement (이름, 접두사, 내용)로 만듭니다. 접두어는 XElementAttribute입니다. – womp

+0

예, "rss"행의 속성에 포함하여 구현했습니다. '새로운 XAttribute (XNamespace.Xmlns + "g", xnamespace)'. 후속 XElements는 네임 스페이스 + 실제 요소 이름으로 명명되어야합니다. (내 대답을 참조하십시오.) –