o12d10
o12d10

Reputation: 798

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. When I am writing the entries using the code below the system is throwing an exception of 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:-

<users>
    <user>
        <fname>abc</fname>
        <lname>xyz</lname>
        <dob>MM/DD/YYYY</dob>
        <uid>username</uid>
        <pwd>***</pwd>
        <email>[email protected]</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>[email protected]</email>
        <lastlog>DATE:TIME</lastlog>
    </user>
    <user>
        <fname>bcd</fname>
        <lname>lmo</lname>
        <dob>MM/DD/YYYY</dob>
        <uid>username1</uid>
        <pwd>***</pwd>
        <email>[email protected]</email>
        <lastlog>DATE:TIME</lastlog>
    </user>
</users>

So it want my code to make my file run in the following desired manner.

I am getting the following error on debugging

Upvotes: 1

Views: 1003

Answers (3)

o12d10
o12d10

Reputation: 798

This is the final working code: thanks to @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"));
    }
}

Upvotes: 0

User2012384
User2012384

Reputation: 4919

Try debugging the program.

Remove the try catch part, so that the program will flow the exception

Add a breakpoint to see how the program runs

Try these:

  • does the file App_Data/userlogs.xml exists?
  • is the "doc" object null?

Post more code if possible

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

from the code given there can be one possibility for getting NullReferenceException

1.Please check wether your xml file is available or not in following path:

~/App_Data/userlogs.xml

--> your file should be placed in 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
}

Upvotes: 1

Related Questions