建立一个谷歌产品饲料中的.Net(C#)? [英] Building a Google Product Feed in .Net (C#)?

查看:135
本文介绍了建立一个谷歌产品饲料中的.Net(C#)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我试图符合架构:

Below is the schema I am trying to conform to:

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

下面是我已经能够生产出:

Below is what I have been able to produce:

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

下面是code,我写了实现这一目标(以上):

Below is the code that I wrote to achieve this (the above):

    // 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标记元素......是错误的。我已成功地复制它的很多,但迄今为止的结束标记逃避。你们可以帮忙吗?

Those with eager eyes, will have spotted the problem I am having conforming to the google product feed schema ... "the closing rss tag element" ... is wrong. I have managed to replicate alot of it so far but that closing tag eludes. Can you guys help?

另外,感觉自由地改变我的code如果我做错了什么,或去这样做了错误的方式?干杯。

Also, feel to free to change my code if I have done anything wrong or gone about doing it the wrong way? Cheers.

推荐答案

如果有什么,而不是做这样的:

What if, instead of doing this:

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标签,根据您的code例如后添加的版本属性。 (可能要仔细检查我的语法)

I've never used XmlTextWriter before, but I'd think you should be able to add the version attribute after creating the rss tag, based on your code example. (Might wanna double-check my syntax)

这篇关于建立一个谷歌产品饲料中的.Net(C#)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆