我要写个计数器把数字存放在一个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大家帮忙,谢谢
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大家帮忙,谢谢
intcount.LoadXml("<root>"+
"</root>");后面两行改成
intcount.DocumentElement.AppendChild(XN);
intcount.Save(path);
试试
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;
}
}
}
}
How about using web.config?
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;
}
}
}
}