[英]Building a Google Product Feed in .Net (C#)?
下面是我試圖遵循的架構:
<?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標簽元素”......是錯誤的。 到目前為止,我已經設法復制了很多,但結束標簽卻沒有。 你們能幫忙嗎?
另外,如果我做錯了什么或者以錯誤的方式做錯,我可以隨意更改我的代碼嗎? 干杯。
問題是您正在嘗試創建名稱為rss version="2.0"
的元素。 相反,您應該創建一個名為rss
的元素,並將version
屬性設置為值為2.0
:
xw.WriteStartElement("rss");
xw.WriteAttributeString("version", "2.0");
我個人會使用LINQ to XML而不是XmlWriter
,請注意 - 它是一個更好的API。
如果,而不是這樣做,該怎么辦:
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標簽后添加版本屬性,具體取決於你的代碼示例。 (可能要仔細檢查我的語法)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.