在C# .NET 中 合并两个 XML 文档中的数据

回复论坛中的问题而写的DEMO 。问题地址:http://bbs.csdn.net/topics/390562500?page=1#post-395382320


要把XML2的中国的XML数据合并到XML1中国下面

XML1:

<?xml version="1.0" encoding="utf-8" ?>
<CountryCode>
	<Country NameChs="安哥拉" NameEng="Angola" AbbrL2="AO" AbbrL3="ANG" RegionCode="244" GMT="1" />
	<Country NameChs="阿富汗" NameEng="Afghanistan" AbbrL2="AF" AbbrL3="AFG" RegionCode="93" GMT="8" />
	<Country NameChs="阿尔巴尼亚" NameEng="Albania" AbbrL2="AL" AbbrL3="ALB" RegionCode="355" GMT="1" />
	<Country NameChs="阿尔及利亚" NameEng="Algeria" AbbrL2="DZ" AbbrL3="ALG" RegionCode="213" GMT="0" />
	<Country NameChs="安道尔共和国" NameEng="Andorra" AbbrL2="AD" AbbrL3="AND" RegionCode="376" GMT="0" />
	<Country NameChs="中国" NameEng="China" AbbrL2="CN" AbbrL3="CHS" RegionCode="86" GMT="8" />
</CountryCode>

XML2:

<?xml version="1.0" encoding="utf-8" ?>
<CityIndex>
	<Country Name="中国" NameEng="China" Code="86">
		<StateProvince Name="北京" NameEng="Beijing">
			<City Name="北京市" NameEng="Beijing Shi" PostalCode="100000" />
		</StateProvince>
		<StateProvince Name="天津" NameEng="TienTisn">
			<City Name="天津市" NameEng="TienTisn Shi" PostalCode="300000" />
		</StateProvince>
		<StateProvince Name="河北省" NameEng="HeBei Sheng">
			<City Name="石家庄市" NameEng="ShiJiaZhuang Shi" PostalCode="050000" />
			<City Name="唐山市" NameEng="TangShan Shi" PostalCode="063000" />
			<City Name="秦皇岛市" NameEng="QinHuangDao Shi" PostalCode="066000" />
			<City Name="邯郸市" NameEng="HanDan Shi" PostalCode="056002" />
			<City Name="邢台市" NameEng="XingTai Shi" PostalCode="054000" />
			<City Name="保定市" NameEng="BaoDing Shi" PostalCode="071000" />
			<City Name="张家口市" NameEng="ZhangJiaKou Shi" PostalCode="075000" />
			<City Name="承德市" NameEng="ChengDe Shi" PostalCode="076000" />
			<City Name="沧州市" NameEng="CangZhou Shi" PostalCode="061000" />
			<City Name="廊坊市" NameEng="LangFang Shi" PostalCode="102800" />
			<City Name="衡水市" NameEng="HengShui Shi" PostalCode="053000" />
		</StateProvince>
	</Country>
</CityIndex>

合并代码:

				XmlDocument doc1 = new XmlDocument();
				doc1.Load(Server.MapPath(@"XMLFile1.xml"));
				XmlDocument doc2 = new XmlDocument();
				doc2.Load(Server.MapPath(@"XMLFile2.xml"));
				XmlNode root1 = doc1.DocumentElement;

				//获取所有节点为"CityIndex"的节点
				XmlNodeList xnl = doc2.SelectNodes("CityIndex"); 
				foreach (XmlNode xnItem in xnl)
				{
					XmlNode root2 = doc1.ImportNode(xnItem, true);
					root1.AppendChild(root2);
				}
				doc1.Save(Server.MapPath(@"XMLFile3.xml"));


合并后的效果如下:

<?xml version="1.0" encoding="utf-8"?>
<CountryCode>
  <Country NameChs="安哥拉" NameEng="Angola" AbbrL2="AO" AbbrL3="ANG" RegionCode="244" GMT="1" />
  <Country NameChs="阿富汗" NameEng="Afghanistan" AbbrL2="AF" AbbrL3="AFG" RegionCode="93" GMT="8" />
  <Country NameChs="阿尔巴尼亚" NameEng="Albania" AbbrL2="AL" AbbrL3="ALB" RegionCode="355" GMT="1" />
  <Country NameChs="阿尔及利亚" NameEng="Algeria" AbbrL2="DZ" AbbrL3="ALG" RegionCode="213" GMT="0" />
  <Country NameChs="安道尔共和国" NameEng="Andorra" AbbrL2="AD" AbbrL3="AND" RegionCode="376" GMT="0" />
  <Country NameChs="中国" NameEng="China" AbbrL2="CN" AbbrL3="CHS" RegionCode="86" GMT="8" />
  <CityIndex>
    <Country Name="中国" NameEng="China" Code="86">
      <StateProvince Name="北京" NameEng="Beijing">
        <City Name="北京市" NameEng="Beijing Shi" PostalCode="100000" />
      </StateProvince>
      <StateProvince Name="天津" NameEng="TienTisn">
        <City Name="天津市" NameEng="TienTisn Shi" PostalCode="300000" />
      </StateProvince>
      <StateProvince Name="河北省" NameEng="HeBei Sheng">
        <City Name="石家庄市" NameEng="ShiJiaZhuang Shi" PostalCode="050000" />
        <City Name="唐山市" NameEng="TangShan Shi" PostalCode="063000" />
        <City Name="秦皇岛市" NameEng="QinHuangDao Shi" PostalCode="066000" />
        <City Name="邯郸市" NameEng="HanDan Shi" PostalCode="056002" />
        <City Name="邢台市" NameEng="XingTai Shi" PostalCode="054000" />
        <City Name="保定市" NameEng="BaoDing Shi" PostalCode="071000" />
        <City Name="张家口市" NameEng="ZhangJiaKou Shi" PostalCode="075000" />
        <City Name="承德市" NameEng="ChengDe Shi" PostalCode="076000" />
        <City Name="沧州市" NameEng="CangZhou Shi" PostalCode="061000" />
        <City Name="廊坊市" NameEng="LangFang Shi" PostalCode="102800" />
        <City Name="衡水市" NameEng="HengShui Shi" PostalCode="053000" />
      </StateProvince>
    </Country>
  </CityIndex>
</CountryCode>

XML2里面的节点为

<CityIndex>北京</CityIndex>

这样的话合并代码为

				XmlDocument doc1 = new XmlDocument();
				doc1.Load(Server.MapPath(@"XMLFile1.xml"));
				XmlDocument doc2 = new XmlDocument();
				doc2.Load(Server.MapPath(@"XMLFile2.xml"));
				XmlNode root1 = doc1.DocumentElement;
				XmlNode root2 = doc1.ImportNode(doc2.DocumentElement.FirstChild, true);
				root1.AppendChild(root2);
//如果有多个节点的话可以用下面代码
 foreach(XmlNode n  in doc2.DocumentElement.ChildNodes)
    {
    XmlNode root2 = doc1.ImportNode(n, true);
    root1.AppendChild(root2); 
    }
 doc1.Save(Server.MapPath(@"XMLFile3.xml"));



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值