2010-10-26 16 views
4

下面是我試圖符合該模式以:使用.Net(C#)構建Google Product Feed?

<?xml version="1.0"?> 
    <rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"> 
    <channel> 
    <title>The name of your data feed</title> 
    <link>http://www.example.com</link> 
    <description>A description of your content</description> 
    <item> 
     <title>Red wool sweater</title> 
     <link> http://www.example.com/item1-info-page.html</link> 
     <description>Comfortable and soft ... cold winter nights.</description> 
     <g:image_link>http://www.example.com/image1.jpg</g:image_link> 
     <g:price>25</g:price> 
     <g:condition>new</g:condition> 
     <g:id>1a</g:id> 
    </item> 
    </channel> 
    </rss> 

下面是我已經能夠生產出:

<?xml version="1.0"?> 
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"> 
    <channel> 
    <title>The name of your data feed</title> 
    <link>http://www.google.com</link> 
    <description>A description of your content</description> 
    <item> 
     <title>Red Wool Sweater</title> 
     <link>http://www.google.com/Red-Wool-Sweater</link> 
     <description>Comfortable and soft, this sweater will keep you warm on those cold winter nights.</description> 
     <g:image_link>http://www.example.com/image1.jpg</g:image_link> 
     <g:price>25</g:price> 
     <g:condition>new</g:condition> 
     <g:id>1a</g:id> 
    </item> 
    </channel> 
</rss version="2.0"> 

下面是我寫的,以實現這一目標(在上面的代碼):

// create and instantiate the writer object. 
    XmlTextWriter xw = new XmlTextWriter("Products.xml", null); 

    // use indenting. 
    xw.Formatting = Formatting.Indented; 

    // write the start of the document. 
    xw.WriteStartDocument(); 

    xw.WriteStartElement("rss version=\"2.0\""); 

    xw.WriteAttributeString("xmlns", "g", null, "http://base.google.com/ns/1.0"); 

    xw.WriteStartElement("channel"); 

    xw.WriteElementString("title", "The name of your data feed"); 
    xw.WriteElementString("link", "http://www.google.com"); 
    xw.WriteElementString("description", "A description of your content"); 

    xw.WriteStartElement("item"); 

    xw.WriteElementString("title", "Red Wool Sweater"); 
    xw.WriteElementString("link", "http://www.google.com/Red-Wool-Sweater"); 
    xw.WriteElementString("description", "Comfortable and soft, this sweater will keep you warm on those cold winter nights."); 
    xw.WriteElementString("g:image_link", "http://www.example.com/image1.jpg"); 
    xw.WriteElementString("g:price", "25"); 
    xw.WriteElementString("g:condition", "new"); 
    xw.WriteElementString("g:id", "1a"); 

    // write the end element. 
    xw.WriteEndElement(); 
    xw.WriteEndElement(); 
    xw.WriteEndElement(); 
    // write the end of the document. 
    xw.WriteEndDocument(); 

    // close the writer. 
    xw.Close(); 
    // press enter to exit. 
    Console.ReadKey(); 

那些與渴望的眼睛,會發現有我有符合谷歌產品飼料架構中的問題......「閉幕RSS標籤元素」 ......是錯的。到目前爲止,我已經設法複製了很多,但是結束標記沒有出現。你們能幫忙嗎?

另外,如果我做錯了什麼事或者做了錯誤的方法,是否可以自由地更改我的代碼?乾杯。

+0

跛腳的方法是把它全部放在某種形式的strea中,並替換並重寫最後一行:D – IbrarMumtaz 2010-10-26 14:07:11

回答

2

如果,而不是這樣做:

xw.WriteStartElement("rss version=\"2.0\""); 
xw.WriteAttributeString("xmlns", "g", null, "http://base.google.com/ns/1.0"); 

你做了這樣的事情:

xw.WriteStartElement("rss"); 
xw.WriteAttributeString("version", "2.0"); 
xw.WriteAttributeString("xmlns", "g", null, "http://base.google.com/ns/1.0"); 

我以前從未使用過的XmlTextWriter,但我認爲你應該能夠根據您的代碼示例在創建rss標籤後添加版本屬性。 (可能希望仔細檢查我的語法)

+0

乾杯我會放棄這一切。 – IbrarMumtaz 2010-10-26 14:14:05

6

問題是,您正嘗試創建一個名稱爲的元素rss version="2.0"。相反,你應該建立與rss一個名稱的元素,並設置version屬性有2.0值:

xw.WriteStartElement("rss"); 
xw.WriteAttributeString("version", "2.0"); 

個人而言,我會使用LINQ到XML,而不是XmlWriter,開始時,你要知道 - 這是一個更好的API。

+0

爲評論歡呼,但是你的開發人員比我高,另外當我開始從數據庫中提取數據時,我將使用LINQ作爲後端。 – IbrarMumtaz 2010-10-26 14:28:20