读书人

xml文件的有关问题

发布时间: 2012-04-12 15:46:35 作者: rapoo

求助xml文件的问题
string file = HttpContext.Current.Server.MapPath("~/file/count.xml");
public int add(string sname,string spass,string dt)
{
HyperLink1.NavigateUrl = file;

int cid=0;
try{
XmlDocument xdl=new XmlDocument();

xdl.Load(file);
XmlNode node=xdl.SelectSingleNode("/counts");
if(node!=null)
{
XmlNode last=node.LastChild;
cid=Int32.Parse(last.Attributes["id"].Value)+1;
}
else
{
cid=1;
}
XmlNode child=xdl.CreateNode(XmlNodeType.Element,"count",null);
addattribute(xdl,child,"id",cid.ToString());
addattribute(xdl, child, "name", sname);
addattribute(xdl,child,"passWord",spass);
addattribute(xdl, child, "dateTime", dt);
node.AppendChild(child);
xdl.Save(file);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return cid;

}
private void addattribute(XmlDocument xmldoc, XmlNode parent, string sname, string value)
{
XmlAttribute attribute = xmldoc.CreateAttribute(sname, null);
attribute.Value = value;
parent.Attributes.Append(attribute);
}





protected void Button1_Click(object sender, EventArgs e)
{
try
{
add(TextBox1.Text, TextBox2.Text, System.DateTime.Now.ToString());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
可是 提交总是说无法将对象引入到所制订的实例?

[解决办法]
Object reference not set to an instance of an object.
是这个错误吗?
代码:

C# code
cid=Int32.Parse(last.Attributes["id"].Value)+1;
[解决办法]
把具体的错误是哪一条语句产生的,描述一下

读书人网 >XML SOAP

热点推荐