Создание фида продуктов Google в .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();

Те, у кого нетерпеливые глаза, заметят мою проблему, связанную со схемой фида продуктов Google ... "закрывающий элемент тега rss " ... неправильно. Мне удалось воспроизвести многое из этого, но этот закрывающий тег ускользает. Вы, ребята, можете помочь?

Кроме того, не стесняйтесь изменять свой код, если я сделал что-то не так или сделал это неправильно? Ура.

5
задан Shimmy 16 November 2010 в 23:16
поделиться