各位大歌大姐,我在.net里面修改xml文件时,它只会修改第奇数个子节点,第偶数个节点丢失了,帮我看看这是什么问题
xml文件如下:
<?xml version="1.0" encoding="gb2312"?>
<schools> 
<school-info id="1">
<name>Riverdale 高中</name>
<city>纽约</city>
<a>aaa</a>
<b>bbb</b>
<students>550</students>
</school-info>

<school-info id="2">
<name> Dream Valley 高中 </name>
<city>布法罗</city>
<a>aaa</a>
<b>bbb</b>
<students>650</students>
</school-info>

<school-info id="3">
<name>Sutherland 私立学校 </name>
<city>波士顿</city>
<a>aaa</a>
<b>bbb</b>
<students> 625 </students>
</school-info>
</schools>处理程序代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;
public partial class xml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("E:\\xml.xml");
        XmlNode root = doc.SelectSingleNode("schools");
       
        for (int i = 0; i < root.ChildNodes.Count; i++)
        {
            XmlNode yi_n = doc.CreateElement("学校信息");
            yi_n.Attributes.Append(root.ChildNodes[i].Attributes["id"]);
            for (int j = 0; j < root.ChildNodes[i].ChildNodes.Count;j++ )
            {
                yi_n.AppendChild(root.ChildNodes[i].ChildNodes   [j]);                
            }
            root.ReplaceChild(yi_n, root.ChildNodes[i]);
        }
        doc.Save("E:\\xml.xml");
       
    }
}
修改后的xml文件如下:
<?xml version="1.0" encoding="gb2312"?>
<schools> 
<学校信息 id="1">
<name>Riverdale 高中</name>
<a>aaa</a>
<students>550</students>
</学校信息>

<学校信息 id="2">
<name> Dream Valley 高中 </name>
<a>aaa</a>
<students>650</students>
</学校信息>

<学校信息 id="3">
<name>Sutherland 私立学校 </name>
<a>aaa</a>
<students> 625 </students>
</学校信息>
</schools>

解决方案 »

  1.   

    XmlNode yi_n = doc.CreateElement("学校信息");
                yi_n.Attributes.Append(root.ChildNodes[i].Attributes["id"]);
                for (int j = 0; j < root.ChildNodes[i].ChildNodes.Count;j++ )
                {
                    yi_n.AppendChild(root.ChildNodes[i].ChildNodes   [j]);                
                }
    做进一个函数,然后就会循环调用,只到没节点止.