簡體   English   中英

將2個XML文件合並為相同的元素C#

[英]Merge 2 XML Files Into Same Elements C#

我需要創建一個以城市名稱作為輸入並返回城市的位置,國家和天氣信息的WebService。 事實是,ID,位置,國家/地區位於一個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個不同的文件使我感到困惑。

您可以使用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(); 

您可以根據需要進行任何更改

希望能幫助到你

使用添加

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM