从项目级别向下合并2个XML文件

时间:2012-01-20 11:07:45

标签: xml

我正在尝试合并2个XML文件,我在此网站上查看了类似的案例,但未找到解决方案。

基本上,2个XML文件具有相同的结构但来自2个源,我想将它们组合成一个XML文件并将其保存在本地。 我想要实现的是: 从文件2和文件1中删除标题部分,并创建一个具有相同结构但具有自定义信息的新文件。 仅将项目部分(及其子项目)添加到组合文件中 并且能够在pubDate上对结果进行排序,以便项目排序降序 然后在本地保存文件 所有用C#

说明

文件1:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>File 1</title>
    <link>http://www.somewhere.com/</link>
    <description>Feed from the source</description>
    <image>
      <title>My image</title>
      <link>http://www.somewhere.com/</link>
      <url>http://www.somewhere.com/images/redesign/graphics/logos/logo-small.png</url>
      <width>128</width>
      <height>23</height>
    </image>
    <language>
    </language>
    <ttl>15</ttl>
    <item>
      <title>Article 1</title>
      <pubDate>Fri, 09 Dec 2011 13:24:27 +0100</pubDate>
      <description>Description here</description>
      <guid>http://www.somewhere.com/pressroom/article1</guid>
      <link>http://www.somewhere.com/pressroom/article1</link>
      <dc:creator>Someone</dc:creator>
    </item>
  </channel>
</rss>

文件2:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>File 2</title>
    <link>http://www.somewhere2.com/</link>
    <description>Feed from the other source</description>
    <image>
      <title>My image</title>
      <link>http://www.somewhere2.com/</link>
      <url>http://www.somewhere2.com/images/redesign/graphics/logos/logo-small.png</url>
      <width>128</width>
      <height>23</height>
    </image>
    <language>
    </language>
    <ttl>15</ttl>
    <item>
      <title>Article 2</title>
      <pubDate>Fri, 11 Dec 2011 13:27:27 +0100</pubDate>
      <description>Description here</description>
      <guid>http://www.somewhere2.com/pressroom/article2</guid>
      <link>http://www.somewhere2.com/pressroom/article2</link>
      <dc:creator>Someone else</dc:creator>
    </item>
  </channel>
</rss>

结果文件:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Merged File</title>
    <link>http://www.somewhere3.com/</link>
    <description>Feed from the merged source</description>
    <image>
      <title>My image</title>
      <link>http://www.somewhere3.com/</link>
      <url>http://www.somewhere3.com/images/redesign/graphics/logos/logo-small.png</url>
      <width>128</width>
      <height>23</height>
    </image>
    <language>
    </language>
    <ttl>15</ttl>
    <item>
      <title>Article 2</title>
      <pubDate>Fri, 11 Dec 2011 13:27:27 +0100</pubDate>
      <description>Description here</description>
      <guid>http://www.somewhere2.com/pressroom/article2</guid>
      <link>http://www.somewhere2.com/pressroom/article2</link>
      <dc:creator>Someone else</dc:creator>
    </item>
    <item>
      <title>Article 1</title>
      <pubDate>Fri, 09 Dec 2011 13:24:27 +0100</pubDate>
      <description>Description here</description>
      <guid>http://www.somewhere.com/pressroom/article1</guid>
      <link>http://www.somewhere.com/pressroom/article1</link>
      <dc:creator>Someone</dc:creator>
    </item>
  </channel>
</rss>

1 个答案:

答案 0 :(得分:1)

只需基于xml结构构建类,并使用序列化将两个文件加载到对象中,然后从一个文件中获取项目列表并将它们添加到另一个文件中,然后序列化为xml。至于你必须改变Item类的比较方法,我相信,不确定之前没有做过。