xml文件;<ClassName>
   <ClassName  title="123">
         <ClassName title="456"/>
   </ClassName>
</ClassName>在遍历时找到XML中title属性的值等于456时,就在此节点下追加一个子节点789
如果已存在789时就提示已经存在此节点结果如下:
<ClassName>
   <ClassName  title="123">
         <ClassName title="456">
             <ClassName title="789"/>
   </ClassName>
   </ClassName>
</ClassName>有人能帮我写一下吗,我定的总不对:代码如下:
    public void Class_AddName(string pathfile, string prClass, string classname)
    {
        XmlDataDocument xmlDoc = new XmlDataDocument();
        //载入文件
        try
        {
            xmlDoc.Load(pathfile);
        }
        catch (Exception ex)
        {
            Console.WriteLine("文件载入失败", ex.ToString());
        }        //判断所属目录
        if (prClass =="作为一级目录")
        {            XmlElement classn = xmlDoc.CreateElement("ClassName");
            XmlAttribute xmlattrib=xmlDoc.CreateAttribute("title");
            xmlattrib.InnerText=classname;
            classn.SetAttributeNode(xmlattrib);
            xmlDoc.DocumentElement.AppendChild(classn);            //保存文档
            xmlDoc.Save(pathfile);
        }
        else
        {
            XmlNodeList xmlnodeList = xmlDoc.SelectSingleNode("ClassName").ChildNodes;
            //遍历节点,查找是否有和classname名称相同的节点
            foreach (XmlNode xn in xmlnodeList)//开始
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("title") == classname)
                {
                    HttpContext.Current.Response.Write("<script language=\"javascript\">" + "\n");
                    HttpContext.Current.Response.Write("alert(\"对不起,已有相同类名\")" + "\n</script>");
                }
                else
                {
                    if (xe.GetAttributeNode("title").Value == prClass)
                    {
                        XmlElement classn = xmlDoc.CreateElement("ClassName");
                        XmlAttribute xmlattrib = xmlDoc.CreateAttribute("title");
                        xmlattrib.InnerText = classname;
                        classn.SetAttributeNode(xmlattrib);
                        xe.AppendChild(classn);
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("<script language=\"javascript\">" + "\n");
                        HttpContext.Current.Response.Write("alert(\"对不起,没有找到相应的主分类\")" + "\n</script>");
                    }
                    //保存文档
                    xmlDoc.Save(pathfile);
                }
            }
        }
    }