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

发布于 2024-10-06 23:31:43 字数 1904 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2024-10-13 23:31:44

将所有 UserDoc 引用更改为 UserRootUserRoot 声明之后的引用)。由于该对象是 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_address 而不是 Email_Address(小写“a”),并且您的 XML 在“address”中有 3 个 D(拼写错误)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文