读书人

100分送出去!求XML循环递归添加子节点

发布时间: 2012-02-21 16:26:23 作者: rapoo

100分送出去!求XML循环递归添加子节点问题
a.xml,无子节点

<?xml version= "1.0 " standalone= "yes "?>
<Structrue>
<Departments>
<Department DWBM= "115600200101 " SJDWBM= "geotech "/>
<Department DWBM= "100300200601 " SJDWBM= "115600200101 " />
<Department DWBM= "200000200601 " SJDWBM= "115600200101 " />
<Department DWBM= "100300200601 " SJDWBM= "115600200101 " />
<Department DWBM= "200000200601 " SJDWBM= "115600200101 " />
<Department DWBM= "168200200201 " SJDWBM= "100300200601 " />
<Department DWBM= "188700200301 " SJDWBM= "100300200601 " />
<Department DWBM= "188700200302 " SJDWBM= "188700200301 " />
<Department DWBM= "188700200303 " SJDWBM= "188700200301 " />
<Department DWBM= "188700200501 " SJDWBM= "188700200301 " />
<Department DWBM= "151200200301 " SJDWBM= "100300200601 " />
<Department DWBM= "151200200302 " SJDWBM= "151200200301 " />
<Department DWBM= "226600200301 " SJDWBM= "200000200601 " />
<Department DWBM= "226600200401 " SJDWBM= "226600200301 " />
</Departments>
</Structrue>

现在根据 "SJDWBM "和 "DWBM "两个属性值来判断,如果a节点 "SJDWBM "的值和b节点的 "DWBM "值相同,则a节点为b节点的子节点。
最终得到b.xml文件,
b.xml

<?xml version= "1.0 " standalone= "yes "?>
<Structrue>
<Departments>
<Department DWBM= "115600200101 " SJDWBM= "geotech ">
<Department DWBM= "100300200601 " SJDWBM= "115600200101 " >
<Department DWBM= "168200200201 " SJDWBM= "100300200601 " />
<Department DWBM= "188700200301 " SJDWBM= "100300200601 " >
<Department DWBM= "188700200302 " SJDWBM= "188700200301 " />
<Department DWBM= "188700200303 " SJDWBM= "188700200301 " />
<Department DWBM= "188700200501 " SJDWBM= "188700200301 " />
</Department>
<Department DWBM= "151200200301 " SJDWBM= "100300200601 " >
<Department DWBM= "151200200302 " SJDWBM= "151200200301 " />
</Department>
</Department>


<Department DWBM= "200000200601 " SJDWBM= "115600200101 " >
<Department DWBM= "226600200301 " SJDWBM= "200000200601 " >
<Department DWBM= "226600200401 " SJDWBM= "226600200301 " />
</Departments>
</Departments>
</Department>
</Department>
</Structrue>


[解决办法]
楼上正解
[解决办法]
我晕死.shoutor
[解决办法]
private void Page_Load(object sender, System.EventArgs e){ Response.Expires = -100;string strXML = @ " <?xml version= " "1.0 " " standalone= " "yes " "?> <Structrue> <Departments> <Department DWBM= " "115600200101 " " SJDWBM= " "geotech " "/> <Department DWBM= " "100300200601 " " SJDWBM= " "115600200101 " " /> <Department DWBM= " "200000200601 " " SJDWBM= " "115600200101 " " /> <Department DWBM= " "100300200601 " " SJDWBM= " "115600200101 " " /> <Department DWBM= " "200000200601 " " SJDWBM= " "115600200101 " " /> <Department DWBM= " "168200200201 " " SJDWBM= " "100300200601 " " /> <Department DWBM= " "188700200301 " " SJDWBM= " "100300200601 " " /> <Department DWBM= " "188700200302 " " SJDWBM= " "188700200301 " " /> <Department DWBM= " "188700200303 " " SJDWBM= " "188700200301 " " /> <Department DWBM= " "188700200501 " " SJDWBM= " "188700200301 " " /> <Department DWBM= " "151200200301 " " SJDWBM= " "100300200601 " " /> <Department DWBM= " "151200200302 " " SJDWBM= " "151200200301 " " /> <Department DWBM= " "226600200301 " " SJDWBM= " "200000200601 " " /> <Department DWBM= " "226600200401 " " SJDWBM= " "226600200301 " " /> </Departments> </Structrue> ";XmlDocument dom = new XmlDocument();dom.LoadXml(strXML);XmlNodeList nl = dom.SelectNodes( "//Department ");for(int i=0;i <nl.Count;i++){ //查找它的子节点,并保存this.GetChildNodes( dom, nl[i]);}dom.Save(Response.OutputStream);Response.End(); }void GetChildNodes( XmlDocument dom, XmlNode node){ XmlNodeList nl =dom.SelectNodes( "//Department[@SJDWBM= ' "+ node.Attributes[ "DWBM "].Value+ " '] ");for(int i=0;i <nl.Count;i++){node.AppendChild(nl[i]);this.GetChildNodes(dom,nl[i]);}}

读书人网 >asp.net

热点推荐