急,System.NullReferenceException: 未将对象引用设置到对象的实例

DongloveRen 2009-06-07 07:56:00
我在做向一个xml中插入记录的程序,出错!
xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<StudentInfoManagementSystem>
<StudentInfos>
<StudentInfo>
<StudentName>dong</StudentName>
<StudentNum>666666</StudentNum>
<StudentGender>man</StudentGender>
<StudentAddress>Tonghai</StudentAddress>
<StudentBirth>12345678</StudentBirth>
<StudentTel>12345678912</StudentTel>
<DepartNum>08</DepartNum>
<ClassNum>062</ClassNum>
<ScoreInfos>
<ScoreInfo>
<CourseNum>11111111</CourseNum>
<Score>88</Score>
</ScoreInfo>
</ScoreInfos>
</StudentInfo>
</StudentInfos>
</StudentInfoManagementSystem>

程序代码:

public partial class StudentManagementSystem : Form
{
public static XmlDocument myXmlDoc = new XmlDocument();
public StudentManagementSystem()
{
InitializeComponent();
}

private void addButton_Click(object sender, EventArgs e)
{
string filePath = Application.StartupPath + "../../../XmlDoc/StudentManagementSystem.xml";

myXmlDoc.Load(filePath);
string name = nameTextBox.Text;
string num = numberTextBox.Text;
string sex = sexTextBox.Text;
string address = addressTextBox.Text;
string birthday = birthTextBox.Text;

XmlNode myNode = myXmlDoc.SelectSingleNode("StudentInfos");

XmlElement myXmlElementNode = myXmlDoc.CreateElement("StudentInfo");
//XmlNode myXmlElementNode = myXmlDoc.CreateNode

XmlElement myXmlElemrntName = myXmlDoc.CreateElement("StudentName");
myXmlElemrntName.InnerText = name;
XmlElement myXmlElementNum = myXmlDoc.CreateElement("StudentNum");
myXmlElementNum.InnerText = num;
XmlElement myXmlElementSex = myXmlDoc.CreateElement("StudentGender");
myXmlElementSex.InnerText = sex;
XmlElement myXmlElementAddress = myXmlDoc.CreateElement("StudentAddress");
myXmlElementAddress.InnerText = address;
XmlElement myXmlElementBirthday = myXmlDoc.CreateElement("StudentBirth");
myXmlElementBirthday.InnerText = birthday;

myXmlElementNode.AppendChild(myXmlElemrntName);
myXmlElementNode.AppendChild(myXmlElementNum);
myXmlElementNode.AppendChild(myXmlElementSex);
myXmlElementNode.AppendChild(myXmlElementAddress);
myXmlElementNode.AppendChild(myXmlElementBirthday);
//////////////////////////////////////////////////////////////////
////////////////////////55就是下面这/////////////////////////////////////////
myNode.AppendChild(myXmlElementNode);
//////////////////////////////////////////////////////////////////////////
myXmlDoc.Save(filePath);
MessageBox.Show("添加成功!");
}
}

错误:
************* 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 StudentManagementSystem.StudentManagementSystem.addButton_Click(Object sender, EventArgs e) 位置 D:\\StudentManagementSystem\Form1.cs:行号 55
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
...全文
226 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
皓月明 2009-06-08
  • 打赏
  • 举报
回复
问题解决啦,要从根节点找起
wuyq11 2009-06-07
  • 打赏
  • 举报
回复
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(Server.MapPath("data.xml"));
XmlNode root=xmlDoc.SelectSingleNode("/StudentInfoManagementSystem/StudentInfos");
XmlElement xe1=xmlDoc.CreateElement("StudentInfo");

XmlElement xesub1=xmlDoc.CreateElement("StudentName");
xesub1.InnerText="";
xe1.AppendChild(xesub1);
XmlElement xesub2=xmlDoc.CreateElement("StudentNum");
xesub2.InnerText="";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("StudentGender");
xesub3.InnerText="";
xe1.AppendChild(xesub3);

root.AppendChild(xe1);
xmlDoc.Save ( Server.MapPath("data.xml") );

DongloveRen 2009-06-07
  • 打赏
  • 举报
回复
问题已经解决,谢谢!
冷月孤峰 2009-06-07
  • 打赏
  • 举报
回复
XmlNode myNode = myXmlDoc.SelectSingleNode("StudentInfos");改成:
XmlNode myNode = myXmlDoc.SelectSingleNode("/StudentInfoManagementSystem/StudentInfos");
XIAODONGXIAN 2009-06-07
  • 打赏
  • 举报
回复
XmlNode myNode = myXmlDoc.SelectSingleNode("/StudentInfoManagementSystem/StudentInfos");
这样应该就行了
wartim 2009-06-07
  • 打赏
  • 举报
回复
XmlNode myNode = myXmlDoc.SelectSingleNode("/StudentInfoManagementSystem/StudentInfos");
DongloveRen 2009-06-07
  • 打赏
  • 举报
回复
求解!

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧