【问题标题】:Merge 2 XML Files Into Same Elements C#将 2 个 XML 文件合并到相同的元素中 C#
【发布时间】:2016-02-14 23:38:03
【问题描述】:

我需要创建一个 WebService,它以城市名称作为输入并返回城市的位置、国家和天气信息。问题是 ID、位置、国家在一个 XML 文件中,而所有天气详细信息在另一个文件中。

<City>
<ID>city1</ID>
<Grid_ref>NG 895608</Grid_ref>
<Name>Berlin</Name>
<Country>Germany</Country>
</City>
<cityWeather>
<ID>city1</ID>
<temperature>20</temperature>
<wind>2</wind>
</cityWeather>

使用 c# 是否可以使用 ID 将所有内容合并到 1 个文件中,还是有其他方法可以做到这一点?然后我会搜索 XML 文件一次,因为有 2 个不同的文件会混淆我。

【问题讨论】:

  • 是的,这是可能的。 “最佳”方式取决于您正在进行的其他 XML 处理(.NET 有多个 XML 解析器 API,然后是 XSLT……)

标签: c# xml wcf http soa


【解决方案1】:

您可以使用数据集。我想你有两个 XML 文件。 CityWeather.xml et City.xml,你可以做这个

try
    {
        XmlTextReader xmlreader1 = new XmlTextReader("C:\\Books1.xml");
        XmlTextReader xmlreader2 = new XmlTextReader("C:\\Books2.xml");

        DataSet ds = new DataSet();
        ds.ReadXml(xmlreader1);
        DataSet ds2 = new DataSet();
        ds2.ReadXml(xmlreader2);
        ds.Merge(ds2);
        ds.WriteXml("C:\\Books.xml");
        Console.WriteLine("Completed merging XML documents");
    }
    catch (System.Exception ex)
    {
        Console.Write(ex.Message);
    }
Console.Read(); 

您可以根据需要进行任何更改

希望对你有帮助

【讨论】:

  • 是的,谢谢。那么上面会根据他们的ID将他们放在同一个标签中吗?
  • books.xml 必须为空吗?我已经用 City.Xml 和 CityWeather.Xml 替换了 books1/books2,将它们添加到一个空白的 CityMerged.Xml..但是城市合并仍然是空白的?
  • 我把这个放在 CityServices.cs 中,program.cs 运行服务器没有错误。但是它仍然是空白的,并且不包括所有细节。
【解决方案2】:

使用加法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string cityXML =
            "<Root>" +
                "<City>" +
                    "<ID>city1</ID>" +
                    "<Grid_ref>NG 895608</Grid_ref>" +
                    "<Name>Berlin</Name>" +
                    "<Country>Germany</Country>" +
                "</City>" +
                "<City>" +
                    "<ID>city2</ID>" +
                    "<Grid_ref>F 5608</Grid_ref>" +
                    "<Name>Paris</Name>" +
                    "<Country>France</Country>" +
                "</City>" +
                "<City>" +
                    "<ID>city3</ID>" +
                    "<Grid_ref>RR 608</Grid_ref>" +
                    "<Name>Rome</Name>" +
                    "<Country>Italy</Country>" +
                "</City>" +
             "</Root>";


            XElement cities = XElement.Parse(cityXML);

            string weatherXML =
            "<Root>" +
                "<cityWeather>" +
                    "<ID>city1</ID>" +
                    "<temperature>20</temperature>" +
                    "<wind>2</wind>" +
                "</cityWeather>" +
                "<cityWeather>" +
                    "<ID>city2</ID>" +
                    "<temperature>30</temperature>" +
                    "<wind>3</wind>" +
                "</cityWeather>" +
                "<cityWeather>" +
                    "<ID>city3</ID>" +
                    "<temperature>40</temperature>" +
                    "<wind>4</wind>" +
                "</cityWeather>" +
            "</Root>";

            XElement weather = XElement.Parse(weatherXML);

            List<XElement> cityList = cities.Descendants("City").ToList(); 
            foreach(XElement city in cityList)
            {
                XElement matchedCity = weather.Descendants("cityWeather").Where(x =>
                    x.Element("ID").Value == city.Element("ID").Value).FirstOrDefault();
                if(matchedCity != null) city.Add(matchedCity);
            }
        }
    }
}
​

【讨论】:

  • 我必须为所有城市都这样做吗?有不少
  • 使用 XL Linq 轻松处理多个城市。需要查看多个城市的示例,其中两个 XML 中的城市名称相同。
  • 不幸的是,我对链接不太熟悉,不会自己管理。我认为上面的 Fatiha 示例很好,虽然它似乎并没有真正起作用
  • xml 文件包含大约 50 个其他城市,它们都有自己的天气 city1、city2、city3 - 我想在搜索 Paris 时合并我想返回 Paris 的所有细节 - 找到在 city.xml 和 cityweather.xml city1NG 895608BerlinGermanycity2F 5608ParisFrancecity3RR 608 RomeItaly
  • 我更新了代码以展示如何合并两个 xml 的示例。您没有发布多个天气 xml,所以我创建了一个包含城市名称的 xml。 XML 城市包含合并的文件。
猜你喜欢
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
相关资源
最近更新 更多