代码之家  ›  专栏  ›  技术社区  ›  IbrarMumtaz

在.NET(c)中构建Google产品提要?

  •  5
  • IbrarMumtaz  · 技术社区  · 14 年前

    下面是我试图遵循的模式:

      <?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标记元素“…是错误的。到目前为止,我已经成功地复制了大量的内容,但最后的标签却被忽略了。你们能帮忙吗?

    另外,如果我做了任何错误的事情或者以错误的方式做了任何事情,您是否可以自由地更改我的代码?干杯。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Colin O'Dell    14 年前

    如果,而不是这样做:

    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标记后根据代码示例添加version属性。(可能要仔细检查我的语法)

        2
  •  6
  •   Jon Skeet    14 年前

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

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

    我个人会使用linq to xml而不是 XmlWriter 首先,请注意-这是一个更好的API。

    推荐文章