日期:2014-05-17  浏览次数:21163 次

XML 节点的复制

现在有一个xml文件,xml.xml
 格式如下
 <root>
 ......
        <setting t="508dee06" n="3"> 
           <mac.00.00.00.00.00.00 t="4d339a19" n="1" >
              <system t="4d339a1a" n="1"> 
               </system>
              <module t="4d339a22" n="2" />
              <plugin t="4d339a23" n="3">
                <bs t="4d339a24" n="1"/>
                 <Cf t="4d7b0d9e" n="1" />
                </bs>
                <fa_songs_max t="4d8a9eb2" n="1"/>
                <CD t="4d7b0d9e" n="1" />
              </plugin> 
           </mac.00.00.00.00.00.00>
           <mac.00.00.00.00.00.11>
           </mac.00.00.00.00.00.11>
            <module t="508dee06" n="4">
              <mac.00.f1.f3.09.46.ed t="4ef9801f" n="1">
            </module>
            <mac.00.f1.f3.07.26.e4 t="4d835def" n="1"/>
          </setting>
 .....
 </root>


需要将<mac.00.00.00.00.00.00>节点下 <plugin t="4d339a23" n="3">下的<bs t="4d339a24" n="1"/>下的Cf节点复制到<mac.00.00.00.00.00.11>对应的结构,,,没有plugin ,bs 需要添加到<mac.00.00.00.00.00.11>中
------最佳解决方案--------------------
		XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(@"c:\test.xml");
XmlElement element = xmlDoc.SelectSingleNode(@"//mac.00.00.00.00.00.00/plugin").CloneNode(true) as XmlElement;
xmlDoc.SelectSingleNode(@"//mac.00.00.00.00.00.11").AppendChild(element);
xmlDoc.Save(@"c:\result.xml");

------其他解决方案--------------------
    
string xmlpath="xml路径";
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlpath);
xmlnode xnmac00=myXmlDoc.SelectSingleNode("mac.00.00.00.00.00.00");
xmlnode xn=xnmac00.ChildNodes["此处为bs的路径"];
 XmlNode xnclone = xn.Clone();
xmlnode xnmac11=myXmlDoc.SelectSingleNode("mac.00.00.00.00.00.11");
xnmac11.AppendChild(xnclone);