我在做向一个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)

解决方案 »

  1.   

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

  2.   

    XmlNode myNode = myXmlDoc.SelectSingleNode("/StudentInfoManagementSystem/StudentInfos");
    这样应该就行了
      

  3.   

                XmlNode myNode = myXmlDoc.SelectSingleNode("StudentInfos");改成:
    XmlNode myNode = myXmlDoc.SelectSingleNode("/StudentInfoManagementSystem/StudentInfos");
      

  4.   

    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") );