在.Net(C#)中构建Google商品Feed?

Ibr*_*taz 5 c# xml rss .net-3.5

下面是我试图遵循的架构:

  <?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>
Run Code Online (Sandbox Code Playgroud)

以下是我能够制作的:

<?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">
Run Code Online (Sandbox Code Playgroud)

下面是我为实现这个目的而写的代码(上面):

    // 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();
Run Code Online (Sandbox Code Playgroud)

那些热切的眼睛,会发现我符合谷歌产品饲料架构的问题......"关闭rss标签元素"......是错误的.到目前为止,我已经设法复制了很多,但结束标签却没有.你们能帮忙吗?

另外,如果我做错了什么或者以错误的方式做错,我可以随意更改我的代码吗?干杯.

Jon*_*eet 6

问题是你正在尝试创建一个名称为的元素rss version="2.0".相反,您应该创建一个名称为的元素rss,并将该version属性设置为值2.0:

xw.WriteStartElement("rss");
xw.WriteAttributeString("version", "2.0");
Run Code Online (Sandbox Code Playgroud)

就个人而言,我会使用LINQ to XML而不是XmlWriter开头,请注意 - 它是一个更好的API.


Col*_*ell 3

如果不这样做怎么办:

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

你做了这样的事情:

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

我以前从未使用过 XmlTextWriter,但我认为您应该能够根据您的代码示例在创建 rss 标记后添加版本属性。(可能想仔细检查我的语法)