开发者

NullReferenceException while Reading XML File with Linq (C# 4.0)

开发者 https://www.devze.com 2023-01-30 18:38 出处:网络
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

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>sam开发者_StackOverflowple123</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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号