使用 Linq 读取 XML 文件时出现 NullReferenceException (C# 4.0)
我完全被难住了,我以前使用过非常相似的代码并且它运行得很好,其中的 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>[email protected]</Email_adddress>
</User>
UserType
类看起来像这样
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'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>[email protected]</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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将所有
UserDoc
引用更改为UserRoot
(UserRoot
声明之后的引用)。由于该对象是XDocument
而不是XElement
,因此您需要在该级别进行操作。否则,您可以参考UserDoc.Root.Element(...)
来代替,但这会更长。另外,请注意您的情况。使用
BirthDate
而不是Birthdate
(大写“D”以匹配您的 XML)。同样,它是Email_address
而不是Email_Address
(小写“a”),并且您的 XML 在“address”中有 3 个 D(拼写错误)。Change all the
UserDoc
references toUserRoot
(the ones after theUserRoot
declaration). Since the object is anXDocument
rather than anXElement
you need to operate at that level. Otherwise you can refer toUserDoc.Root.Element(...)
instead but that's lengthier.Also, be aware of your case. Use
BirthDate
instead ofBirthdate
(capital "D" to match your XML). Similarly, it'sEmail_address
notEmail_Address
(lowercase "a") and your XML has 3 D's in "address" (spelling mistake).