Visual studio 2010 使用Linq(C#4.0)读取XML文件时出现NullReferenceException

Visual studio 2010 使用Linq(C#4.0)读取XML文件时出现NullReferenceException,visual-studio-2010,c#-4.0,linq-to-xml,nullreferenceexception,Visual Studio 2010,C# 4.0,Linq To Xml,Nullreferenceexception,我完全被难住了,我以前使用过非常类似的代码,它工作得非常好,这个程序中的XML是由一个单独的方法编写的,我对照它进行了检查,它看起来很好 这是解析XML文件的代码 UserType CurrentUser = new UserType(); XDocument UserDoc = XDocument.Load(Path2UserFile); XElement UserRoot = UserDoc.Element("User"); CurrentUser.User_ID = int.Parse(

我完全被难住了,我以前使用过非常类似的代码,它工作得非常好,这个程序中的XML是由一个单独的方法编写的,我对照它进行了检查,它看起来很好

这是解析XML文件的代码

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;
Path2UserFile
也指向正确的文件,我让它写出完整的路径

每当它试图解析任何元素的内容时,都会出现NullReferenceException

XML文件遵循这种格式

<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>

我不知道出了什么问题,非常感谢您提供任何帮助

将所有
UserDoc
引用更改为
UserRoot
(在
UserRoot
声明之后的引用)。由于对象是
XDocument
而不是
XElement
,因此需要在该级别进行操作。否则,您可以改为引用
UserDoc.Root.Element(…)
,但这要长一些

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;

另外,要注意你的情况。使用
BirthDate
而不是
BirthDate
(大写“D”以匹配XML)。类似地,它是
Email\u address
而不是
Email\u address
(小写字母“a”),并且您的XML在“address”(拼写错误)中有3个D。

@Indebi没有问题。请再读一遍我的答案。自从你发表评论以来,我还指出了一些其他问题。
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;