Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3492270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:44:17+00:00 2026-05-18T11:44:17+00:00

I’m completely stumped, I’ve used very similar code before and it worked perfectly, the

  • 0

I’m completely stumped, I’ve used very similar code before and it worked perfectly, the XML in this was written by a separate method in this program and I checked it against it and it looked fine

That’s the code for parsing the XML file

UserType CurrentUser = new UserType();
XDocument UserDoc = XDocument.Load(Path2UserFile);

XElement UserRoot = UserDoc.Element("User");
CurrentUser.User_ID = int.Parse(UserDoc.Element("User_ID").Value);
CurrentUser.Full_Name = UserDoc.Element("Full_Name").Value;
CurrentUser.Gender = UserDoc.Element("Gender").Value;
CurrentUser.BirthDate = DateTime.Parse(UserDoc.Element("Birthdate").Value);
CurrentUser.PersonType = int.Parse(UserDoc.Element("PersonType").Value);
CurrentUser.Username = UserDoc.Element("Username").Value;
CurrentUser.Password = UserDoc.Element("Password").Value;
CurrentUser.Email_Address = UserDoc.Element("Email_Address").Value;

The Path2UserFile points to the correct file as well, and I had it write out the full path.

It has a NullReferenceException whenever it tries to parse the contents of any of the elements

The XML File follows this format

<User>
  <User_ID>11</User_ID>
  <Full_Name>Sample User</Full_Name>
  <Gender>Male</Gender>
  <BirthDate>12/12/2010 12:00:00 AM</BirthDate>
  <PersonType>2</PersonType>
  <Username>Sample User</Username>
  <Password>sample123</Password>
  <Email_adddress>sampleuser@gmail.com</Email_adddress>
</User>

The UserType class looks like this

class UserType
{
        public int User_ID = 0;
        public string Full_Name = string.Empty;
        public string Gender = string.Empty;
        public DateTime BirthDate;
        public int PersonType = 0;
        public string Username = string.Empty;
        public string Password = string.Empty;
        public string Email_Address = string.Empty;
}

I have no clue as to what’s wrong, any help would be very much appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:44:18+00:00Added an answer on May 18, 2026 at 11:44 am

    Change all the UserDoc references to UserRoot (the ones after the UserRoot declaration). Since the object is an XDocument rather than an XElement you need to operate at that level. Otherwise you can refer to UserDoc.Root.Element(...) instead but that’s lengthier.

    UserType CurrentUser = new UserType();
    XDocument UserDoc = XDocument.Load(Path2UserFile);
    
    XElement UserRoot = UserDoc.Root;
    CurrentUser.User_ID = int.Parse(UserRoot.Element("User_ID").Value);
    CurrentUser.Full_Name = UserRoot.Element("Full_Name").Value;
    CurrentUser.Gender = UserRoot.Element("Gender").Value;
    CurrentUser.BirthDate = DateTime.Parse(UserRoot.Element("BirthDate").Value);
    CurrentUser.PersonType = int.Parse(UserRoot.Element("PersonType").Value);
    CurrentUser.Username = UserRoot.Element("Username").Value;
    CurrentUser.Password = UserRoot.Element("Password").Value;
    CurrentUser.Email_Address = UserRoot.Element("Email_address").Value;
    

    Also, be aware of your case. Use BirthDate instead of Birthdate (capital “D” to match your XML). Similarly, it’s Email_address not Email_Address (lowercase “a”) and your XML has 3 D’s in “address” (spelling mistake).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.