首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

XML 节点的复制解决方案

2012-12-17 
XML 节点的复制现在有一个xml文件,xml.xml 格式如下 root ......setting t508dee06 n3mac.00.0

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


其他的类似,可以自己试试……


[其他解释]
你的问题就是new一个node,将信息添加,然后在指定节点下add进行,看xmldocument的帮助
[其他解释]
没有plugin ,bs 需要添加到<mac.00.00.00.00.00.11>中

这句什么意思,说得清楚点。
[其他解释]
XmlDocument.CloneNode 方法复制要添加的节点

http://msdn.microsoft.com/zh-tw/library/system.xml.xmldocument.clonenode(v=vs.90)
[其他解释]

引用:
没有plugin ,bs 需要添加到<mac.00.00.00.00.00.11>中

这句什么意思,说得清楚点。


就是<mac.00.00.00.00.00.11>中没有那两个节点 
需要这样
           <mac.00.00.00.00.00.00>
               <plugin t="4d339a23" n="3">
                 <bs t="4d339a24" n="1"/>
                  <Cf t="4d7b0d9e" n="1" />
                 </bs>
                 <CD t="4d7b0d9e" n="1" />
               </plugin>
           <mac.00.00.00.00.00.00>
而不是<mac.00.00.00.00.00.11>
<Cf t="4d7b0d9e" n="1" />
<CD t="4d7b0d9e" n="1" />
</mac.00.00.00.00.00.11>
[其他解释]
引用:
C# code?123456789     string xmlpath="xml路径";XmlDocument myXmlDoc = new XmlDocument();myXmlDoc.Load(xmlpath);xmlnode xnmac00=myXmlDoc.SelectSingleNode("mac.00.00.00.00.00.00");xmlnode xn=……


好像就是这样的
[其他解释]
引用:
C# code?123456XmlDocument xmlDoc = new XmlDocument();xmlDoc.PreserveWhitespace = true;xmlDoc.Load(@"c:\test.xml");XmlElement element = xmlDoc.SelectSingleNode(@"//mac.00.00.00.00.00.00/pl……


这样的话有多条也会被复制的,我只想复制一条
[其他解释]

            string middleNd = "";
             string lNode = "";
 
             XmlNode root = xmlDoc.SelectSingleNode(@"//"+whatMacMyPas+"/"+cpSPath);//得到配置根节点
             if (root == null)
             {
                 string[] sArray = Regex.Split(cpSPath, "/", RegexOptions.IgnoreCase);
                 string sysplu = sArray[0];


 
                 if (sArray.Length <= 2)
                 {
                     lNode = sArray[1];
                     string np = "/root/config/client/pface/setting/" + whatMacMyPas + "/" + sysplu;
                     XmlNode rootXml = xmlDoc.SelectSingleNode(np);
                     XmlElement xe = xmlDoc.CreateElement(lNode);
                     xe.SetAttribute("t", "");
                     xe.SetAttribute("n", "1");
                     xe.SetAttribute("h", "4");
                     xe.SetAttribute("f", "0");
                     xe.SetAttribute("d", cpSValue);
                     rootXml.AppendChild(xe);
                 }
                 if (sArray.Length > 2)
                 {
                     middleNd = sArray[1];
                     lNode = sArray[2];//
                     string np = "/root/config/client/pface/setting/" + whatMacMyPas + "/" + sysplu;
                     XmlNode rootXml = xmlDoc.SelectSingleNode(np);
                     XmlElement xe = xmlDoc.CreateElement(middleNd);
                     xe.SetAttribute("t", "");
                     xe.SetAttribute("n", "1");
                     xe.SetAttribute("h", "4");
                     xe.SetAttribute("f", "0");
                     xe.SetAttribute("d", "");


                     XmlElement ln = xmlDoc.CreateElement(lNode);
                     ln.SetAttribute("t", "");
                     ln.SetAttribute("n", "1");
                     ln.SetAttribute("h", "4");
                     ln.SetAttribute("f", "0");
                     ln.SetAttribute("d", cpSValue);
                     xe.AppendChild(ln);
                     rootXml.AppendChild(xe);
                 }
             }
             else if (root != null)
             {
                 root.Attributes["d"].Value = cpSValue;
             



自己用了个比较笨的办法 但是有效

热点排行