我要写个计数器把数字存放在一个xml里面就存一个数就可以我现在写的:                    XmlDocument intcount = new XmlDocument();
                    intcount.Load(path);                    XmlNode XN = intcount.CreateElement("count");
                    XN.InnerText = "0";
                    string countmum = XN.InnerText;
             if (ifadd == "yes")
                    {
                        XN.InnerText = "11";
                    }
                    else
                    {
                        XN.InnerText = "22";
                    }
                    intcount.Save(path);
                    return true;可是总是报错Root element is missing大家帮忙,谢谢

解决方案 »

  1.   

    XML文档一定要有一个根节点,你这样添加节点法还有视根节点的存在吗?
      

  2.   

    把你所有代码都发上来看看啊,,看提示应该是你那个xml文件是空的,,你把第二行先改成
    intcount.LoadXml("<root>"+
    "</root>");后面两行改成
    intcount.DocumentElement.AppendChild(XN);
    intcount.Save(path);
    试试
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Xml;
    using System.Data;
    using System.Web;namespace count
    {
        public class Class1
        {
            public string countread(ref string Err,string path)
            {
                
                try
                {
                    int i = 0;
                    if (File.Exists(path) == false)
                    {
                        File.Create(path);
                        XmlDocument intcount = new XmlDocument();
                        XmlNode XN = intcount.CreateElement("count");
                        intcount.AppendChild(XN);
                        intcount.Load(path);
                        
                        XN.InnerText = (i + 1).ToString();
                        intcount.Save(path);
                        return XN.InnerText;
                    }
                    else
                    {
                        XmlDocument intcount = new XmlDocument();
                        XmlNode XN = intcount.CreateElement("count");
                        intcount.AppendChild(XN);
                        intcount.Load(path);
                        
                        XN.InnerText = (i + 1).ToString();
                        intcount.Save(path);
                        return XN.InnerText;
                    }
                }
                catch (System.Exception E)
                {
                    Err = Err + E;
                    return Err;
                }
            }        public bool countwrite(string ifadd, ref string Err, string path)
            {
                try
                {
                    if (File.Exists(path) == false)
                    {
                        File.Create(path);
                        return true;
                    }
                    else
                    {
                        
                        XmlDocument intcount = new XmlDocument();
                        XmlNode XN = intcount.CreateElement("count");
                        intcount.AppendChild(XN);
                        intcount.Load(path);
                        
                        XN.InnerText = "0";
                        string countmum = XN.InnerText;                    if (ifadd == "yes")
                        {
                            XN.InnerText = Convert.ToString((Convert.ToInt32(countmum) + 1));
                            //XN.InnerText = "1234124141";
                        }
                        else
                        {
                            XN.InnerText = Convert.ToString((Convert.ToInt32(countmum) + 1));
                        }
                        intcount.Save(path);
                        return true;
                    }
                }
                catch (System.Exception E)
                {
                    Err = Err + E;
                    return false;
                }
            }
        }
    }
      

  4.   

    Do you use it as website counter?
    How about using web.config?
      

  5.   

    yes, as a counter.what`s the matter with web.congif?
      

  6.   

    自己搞定了,谢谢大家,公布一下代码,初试xml,不足的望大家指点函数名字起的基本就是功能的意义,因为很简单就不写注释了^^using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Xml;
    using System.Data;
    using System.Web;namespace count
    {
        public class Class1
        {
            public string countread(ref string Err,string path)
            {
                
                try
                {
                    int i = 0;
                    if (File.Exists(path) == false)
                    {
                        BuildXml(ref Err,path);
                        return i.ToString();
                    }
                    else
                    {
                        XmlDocument intcount = new XmlDocument();
                        intcount.Load(path);
                        XmlNode xnn = intcount.SelectSingleNode("count");
                        i =Int32.Parse(xnn.InnerText.ToString().Trim());
                        intcount.Save(path);
                        return i.ToString();
                    }
                }
                catch (System.Exception E)
                {
                    Err = Err + E;
                    return Err;
                }
            }        public bool countwrite(string ifadd, ref string Err, string path)
            {
                try
                {
                    if (File.Exists(path) == false)
                    {
                        BuildXml(ref Err,path);
                        return true;
                    }
                    else
                    {
                        XmlDocument intcount = new XmlDocument();
                        intcount.Load(path);
                        XmlNode XN = intcount.DocumentElement;
                        
                        string countmum = XN.InnerText;                    if (ifadd == "yes")
                        {
                            XN.InnerText = Convert.ToString((Convert.ToInt32(countmum) + 1));
                        }
                        else
                        {
                            XN.InnerText = Convert.ToString(Convert.ToInt32(countmum));
                        }
                        intcount.Save(path);
                        return true;
                    }
                }
                catch (System.Exception E)
                {
                    Err = Err + E;
                    return false;
                }
            }        private bool BuildXml(ref string Err, string path)
            {
                try
                {
                    XmlDocument xd = new XmlDocument();
                    XmlTextWriter xw = new XmlTextWriter(path, UTF8Encoding.UTF8);
                    xw.WriteStartDocument();
                    xw.WriteStartElement("count");
                    xw.WriteString("0");
                    xw.WriteEndElement();
                    xw.WriteEndDocument();
                    xw.Close();
                    return true;
                    
                }
                catch (System.Exception E)
                {
                    Err = Err + E;
                    return false;
                }
            }   
        
        }
    }