2010-10-26 44 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