[英]System.NullReferenceException: while writing to XML file
I am creating a signup page and the file used to store the user details via regiration form is XML file. 我正在创建一个注册页面,用于通过注册表单存储用户详细信息的文件是XML文件。 When I am writing the entries using the code below the system is throwing an exception of NullReferenceException.
当我使用下面的代码编写条目时,系统抛出NullReferenceException异常。
protected void register_Click(object sender, EventArgs e)
{
try
{
//var path = Path.Combine(Request.PhysicalApplicationPath, "App_Data\\PageData.xml");
XDocument doc = XDocument.Load(Server.MapPath("~/App_Data/userlogs.xml"));
XElement user = new XElement("user",
new XElement("fname", fname.Text.ToString()),
new XElement("lname", lname.Text.ToString()),
new XElement("dob", dob.Text.ToString()),
new XElement("uid", uid.Text.ToString()),
new XElement("pwd", pwd.Text.ToString()),
new XElement("email", email.Text.ToString()),
new XElement("lastlog", System.DateTime.Now.ToString())
);
doc.Root.Element("users").Add(user);
doc.Save("userlog.xml");
}
catch (Exception exe)
{
error.Visible = true;
error.Text = exe.ToString();
}
}
My userslog.xml File format:- 我的userslog.xml文件格式:-
<users>
<user>
<fname>abc</fname>
<lname>xyz</lname>
<dob>MM/DD/YYYY</dob>
<uid>username</uid>
<pwd>***</pwd>
<email>pqrs@xyz.com</email>
<lastlog>DATE:TIME</lastlog>
</user>
</users>
By this code i want to create new tag 通过此代码,我想创建新标签
<users>
<user>
<fname>abc</fname>
<lname>xyz</lname>
<dob>MM/DD/YYYY</dob>
<uid>username</uid>
<pwd>***</pwd>
<email>pqrs@xyz.com</email>
<lastlog>DATE:TIME</lastlog>
</user>
<user>
<fname>bcd</fname>
<lname>lmo</lname>
<dob>MM/DD/YYYY</dob>
<uid>username1</uid>
<pwd>***</pwd>
<email>pqrs@xyz.com</email>
<lastlog>DATE:TIME</lastlog>
</user>
</users>
So it want my code to make my file run in the following desired manner. 因此,它希望我的代码使我的文件以以下期望的方式运行。
from the code given there can be one possibility for getting NullReferenceException
从给出的代码中可以获得
NullReferenceException
一种可能性
1.Please check wether your xml file is available or not in following path: 1.请检查以下路径中的xml文件是否可用:
~/App_Data/userlogs.xml
--> your file should be placed in RootFolder of Project/App_Data/
->您的文件应放在
RootFolder of Project/App_Data/
before proceeding further you can Check wether file exists or not by: 在继续进行操作之前,您可以通过以下方法检查文件是否存在:
System.IO.File.Exists(filepath)
{
//true so file exists
//contine
}
Try debugging the program. 尝试调试程序。
Remove the try catch part, so that the program will flow the exception 删除try catch部分,以便程序将引发异常
Add a breakpoint to see how the program runs 添加断点以查看程序如何运行
Try these: 试试这些:
App_Data/userlogs.xml
exists? App_Data/userlogs.xml
存在? Post more code if possible 如果可能,发布更多代码
This is the final working code: thanks to @sudhakar 这是最终的工作代码:感谢@sudhakar
protected void register_Click(object sender, EventArgs e)
{
//var path = Path.Combine(Request.PhysicalApplicationPath, "App_Data\\PageData.xml");
File.Exists("~/App_Data/userlogs.xml");
{
XDocument doc = XDocument.Load(Server.MapPath("~/App_Data/userlogs.xml"));
XElement user = new XElement("user",
new XElement("fname", fname.Text.ToString()),
new XElement("lname", lname.Text.ToString()),
new XElement("dob", dob.Text.ToString()),
new XElement("uid", uid.Text.ToString()),
new XElement("pwd", pwd.Text.ToString()),
new XElement("email", email.Text.ToString()),
new XElement("lastlog", System.DateTime.Now.ToString())
);
doc.Root.Add(user);
doc.Save(Server.MapPath("~/App_Data/userlogs.xml"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.