C \ #. NET 에서 XML 문서 두 개 를 합 친 데이터

7066 단어 xmlXML 병합
포럼 의 질문 에 답 하여 쓴 데모.질문 주소:http://bbs.csdn.net/topics/390562500?page=1#post-395382320
XML 2 의 중국의 XML 데 이 터 를 XML 1 중국 아래 에 합 쳐 야 합 니 다.
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>

XML 2 의 노드 는
< 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"));

좋은 웹페이지 즐겨찾기