以文本方式查看主题

-  W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWL  (http://bbs.xml.org.cn/index.asp)
--  『 XML 与 数据库 』  (http://bbs.xml.org.cn/list.asp?boardid=17)
----  关于c#遍历节点时定位某一节点的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=17&rootid=&id=31138)


--  作者:fangfei
--  发布时间:4/24/2006 8:57:00 AM

--  关于c#遍历节点时定位某一节点的问题
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.DocumentElement.ChildNodes;
            //遍历节点,查找是否有和classname名称相同的节点
            foreach (XmlNode xn in xmlnodeList)//开始
            {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.GetAttribute("title") == prClass)
                    {
                        XmlNodeList xls = xe.ChildNodes;
                        foreach (XmlNode xn1 in xls)
                        {
                            XmlElement xe1 = (XmlElement)xn1;
                            if (xe1.GetAttribute("title") == classname)
                            {
                                HttpContext.Current.Response.Write("<script language=\"javascript\">" + "\n");
                                HttpContext.Current.Response.Write("alert(\"对不起,已有相同类名\")" + "\n</script>");
                                break;
                            }
                         }
                         XmlElement classn = xmlDoc.CreateElement("ClassName");
                         XmlAttribute xmlattrib = xmlDoc.CreateAttribute("title");
                         xmlattrib.InnerText = classname;
                         classn.SetAttributeNode(xmlattrib);
                         xe.AppendChild(classn);
                         //保存文档
                         xmlDoc.Save(pathfile);
                         break;
                    }

            }
        }
    }


if (xe.GetAttribute("title") == prClass)
好像这一句不进行测试
我在加一个ELSE时提示我要提示的信息,如父节点不存在


--  作者:fangfei
--  发布时间:4/24/2006 9:40:00 AM

--  
没有人会吗
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms