将两个XML文件合并到相同的元素C#中

本文关键字:元素 合并 文件 XML 两个 | 更新日期: 2023-09-27 18:21:44

我需要创建一个以城市名称为输入并返回城市的位置、国家和天气信息的WebService。问题是ID、Location、Country在一个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将所有内容合并到一个文件中,或者是否有其他方法可以做到这一点?然后我会搜索XML文件一次,因为有两个不同的文件会把我搞混。

将两个XML文件合并到相同的元素C#中

您可以使用数据集。我想您有两个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(); 

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

希望它能帮助

使用添加

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);
            }
        }
    }
}
​