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

时间:2015-11-13 11:38:06

标签: c# xml wcf http soa

我需要创建一个WebService,它将城市名称作为输入,并返回城市的位置,国家和天气信息。问题是ID,Location,Country在一个XML文件中,所有天气详细信息在另一个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 个答案:

答案 0 :(得分:1)

您可以使用DataSet。我想你有两个XML文件。 CityWeather.xml和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(); 

您可以进行任何符合您需求的更改

希望有所帮助

答案 1 :(得分:1)

使用添加

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