Building a Google Product Feed in .Net (C#)?

拟墨画扇 提交于 2019-12-07 04:14:22

问题


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

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();

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?

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


回答1:


What if, instead of doing this:

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

You did something like this:

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

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)




回答2:


The problem is that you're trying to create an element with a name of rss version="2.0". Instead, you should be creating an element with a name of rss, and setting the version attribute to have a value of 2.0:

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

Personally I'd use LINQ to XML instead of XmlWriter to start with, mind you - it's a much nicer API.



来源:https://stackoverflow.com/questions/4024482/building-a-google-product-feed-in-net-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!