0 0

各位大虾帮看看这段代码,帮忙解决下5

    全局变量
        private string userNumber = "0";
        private static string UserData = "User.dat";
-----------------------------------------------------------------------------------------------------
      /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="uid">用户 </param>
        /// <param name="pwd">密码 </param>
        /// <param name="rememberMe">是否记住我 </param>
        public static void AddUser(string uid, string pwd, bool rememberMe)
        {
            if (!File.Exists(UserData ))
            {
                File.Create(UserData , 1).Close();
                StreamWriter writer = new StreamWriter(UserData );
                StringBuilder builder = new StringBuilder();
                builder.Append(" <?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                builder.Append(" <root>");
                builder.Append(" </root>");

                writer.Write(builder.ToString());
                writer.Close();
                AddNode(uid, pwd, rememberMe);

            }
            else
            {
                AddNode(uid, pwd, rememberMe);
            }

        }
------------------------------------------------------------------------------------------------------

        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="uid">用户 </param>
        /// <param name="pwd">密码 </param>
        /// <param name="rememberMe">是否记住我 </param>
        private static void AddNode(string uid, string pwd, bool rememberMe)
        {
            bool isAdd = true;
            XmlDataDocument userData = new XmlDataDocument();
            userData.Load(UserData);

            XmlNode node = userData.SelectSingleNode("root");

            for (int i = 0; i < node.ChildNodes.Count; i++)
            {
                if (node.ChildNodes[i].Attributes["user"].Value.Trim() == uid)
                {
                    //更新用户消息
                    if (rememberMe)
                    {
                        node.ChildNodes[i].ChildNodes[0].InnerText = pwd;

                    }
                    else
                    {
                        node.ChildNodes[i].ChildNodes[0].InnerText = "";
                    }
                    userData.Save(UserData );
                    isAdd = false;
                    break;
                }
            }
            if (isAdd)
            {
                XmlNode nodeAtt = userData.CreateNode(XmlNodeType.Attribute, "user", null);
                XmlElement user = userData.CreateElement("user");
                XmlElement epwd = userData.CreateElement("pwd");
                user.Attributes.SetNamedItem(nodeAtt);
                user.Attributes["user"].Value = uid;

                if (rememberMe)
                {
                    epwd.InnerText = pwd;
                }
                else
                {
                    epwd.InnerText = "";
                }
                  userData.AppendChild(epwd); ------执行到此处时抛出异常:此文档已具有“DocumentElement”节点。
                  node.AppendChild(user);
                  userData.Save(UserData );
            }
        }


苦思好久,不知道怎么解决,请高手们帮看看,应该怎样做才好呢?
2009年3月30日 23:15
目前还没有答案

相关推荐

Global site tag (gtag.js) - Google Analytics