做一个关于日程安排的小东西。。新手
public static class User
    {
     public static string name; //姓名
     public static string degree;  //学历
     public static int length_of_schooling;//学制
     public static DateTime enrollment_date;//入学时间
     public static DateTime[] plan_of_default=new DateTime[4];//存放4个时间
     public static DateTime[] plan_of_user=new DateTime[4];//个人设置
     public static int base_on_default; //指向上面的一个时间
     public static int base_on_user;     public static void load_from_xml()
      { 
        XmlDocument xml_doc=new XmlDocument ();
        try{
        xml_doc.Load (@"..\..\setting.xml");
        name=xml_doc.ChildNodes[0].ChildNodes[0].InnerText;
        degree=xml_doc .ChildNodes[0].ChildNodes[1].InnerText;
        length_of_schooling=Convert.ToInt32 (xml_doc .ChildNodes[0].ChildNodes [2].InnerText);
        enrollment_date=Convert.ToDateTime (xml_doc.ChildNodes[0].ChildNodes [3].InnerText);
        for (int i = 0; i <= xml_doc.ChildNodes[1].ChildNodes[0].ChildNodes.Count; i++)
            {
                plan_of_default[i]=Convert.ToDateTime (xml_doc.ChildNodes[1].ChildNodes[1].ChildNodes[i].InnerText);
            }
            for (int i = 0; i <= xml_doc.ChildNodes[1].ChildNodes[1].ChildNodes.Count; i++)
            {
                plan_of_user[i]=Convert.ToDateTime (xml_doc.ChildNodes[1].ChildNodes[1].ChildNodes[i].InnerText);
            }
            base_on_default=Convert.ToInt32 (xml_doc.ChildNodes[3].ChildNodes[0].InnerText);
            base_on_user=Convert.ToInt32 (xml_doc.ChildNodes[3].ChildNodes[1].InnerText);
            }
            catch (NullReferenceException )
            { MessageBox.Show("你的个人信息尚未设置,请设置!"); }
xml文件:
<?xml version="1.0"?>
<!--system information-->
<setting>
<user>
<name></name>
         <education></education>
<length_of_schooling></length_of_schooling>
<enrollment_date></enrollment_date>
</user>
<plan><default_plan>
<T1></T1><T2></T2><T3></T3><T4></T4>
</default_plan>
<user_plan><T1></T1><T2></T2><T3></T3><T4></T4></user_plan>
</plan>
<current_state>
<base_on_default></base_on_default>
<base_on_user></base_on_user>
</current_state>
</setting>其实就是想把下面的xml的内容按号读到类里面。老是说NullReferenceException。。
有高手帮我看看了。谢谢

解决方案 »

  1.   

    XmlDocument xml_doc = new XmlDocument();
                try
                {
                    xml_doc.Load(@"e:\1.xml");
                    name = xml_doc.SelectSingleNode("/setting/user/name").InnerText;
                    degree = xml_doc.SelectSingleNode("/setting/user/education").InnerText;
                    length_of_schooling = xml_doc.SelectSingleNode("/setting/user/length_of_schooling").InnerText;
                    enrollment_date = xml_doc.SelectSingleNode("/setting/user/enrollment_date").InnerText;
                    plan_of_default[0] = xml_doc.SelectSingleNode("/setting/plan/default_plan/T1").InnerText;
                    plan_of_default[1] = xml_doc.SelectSingleNode("/setting/plan/default_plan/T2").InnerText;
                    plan_of_default[2] = xml_doc.SelectSingleNode("/setting/plan/default_plan/T3").InnerText;
                    plan_of_default[3] = xml_doc.SelectSingleNode("/setting/plan/default_plan/T4").InnerText;
                    plan_of_user[0] = xml_doc.SelectSingleNode("/setting/plan/user_plan/T1").InnerText;
                    plan_of_user[1] = xml_doc.SelectSingleNode("/setting/plan/user_plan/T2").InnerText;
                    plan_of_user[2] = xml_doc.SelectSingleNode("/setting/plan/user_plan/T3").InnerText;
                    plan_of_user[3] = xml_doc.SelectSingleNode("/setting/plan/user_plan/T4").InnerText;                base_on_default = xml_doc.SelectSingleNode("/setting/current_state/base_on_default").InnerText;
                    base_on_user = xml_doc.SelectSingleNode("/setting/current_state/base_on_default").InnerText;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
      

  2.   

    上面的代码只是告诉你XPath怎么用
    具体要是转换成int或者DateTime的话
    最好先得到InnerText然后用int.TryParse和DateTime.TryParse转换
      

  3.   

    你可以跟踪调试一下或者参考MSDN,看看ChildNodes是什么概念
    不是你理解的那样~~~~
      

  4.   

    也就是说
    <?xml version="1.0"?>
    <!--system information-->
    <setting>
    都是ChildNode取name应该这样
    name = xml_doc.ChildNodes[2].ChildNodes[0].ChildNodes[0].InnerText;
    你可以测试看一下
      

  5.   

    而且在Convert.ToXXX的时候最好判断一下数据
    是否可以转换
    否则会抛异常
    因此最好用TryParseTryParse的语法可以参考MSDN
      

  6.   

    public static void load_from_xml2()
            {
                XmlDocument xml_doc = new XmlDocument();
                try
                {
                    xml_doc.Load("setting.xml");
                    XmlNode root = xml_doc.ChildNodes[2];
                    name = root.ChildNodes[0].ChildNodes[0].InnerText;
                    degree = root.ChildNodes[0].ChildNodes[1].InnerText;
                    length_of_schooling = Convert.ToInt32(root.ChildNodes[0].ChildNodes[2].InnerText);
                    enrollment_date = Convert.ToDateTime(root.ChildNodes[0].ChildNodes[3].InnerText);
                    for (int i = 0; i <= root.ChildNodes[1].ChildNodes[0].ChildNodes.Count; i++)
                    {
                        plan_of_default[i] = Convert.ToDateTime(root.ChildNodes[1].ChildNodes[1].ChildNodes[i].InnerText);
                    }                for (int i = 0; i <= root.ChildNodes[1].ChildNodes[1].ChildNodes.Count; i++)
                    {
                        plan_of_user[i] = Convert.ToDateTime(root.ChildNodes[1].ChildNodes[1].ChildNodes[i].InnerText);
                    }
                    base_on_default = Convert.ToInt32(root.ChildNodes[3].ChildNodes[0].InnerText);
                    base_on_user = Convert.ToInt32(root.ChildNodes[3].ChildNodes[1].InnerText);
                }
                catch (NullReferenceException)
                {
                    throw new ApplicationException("你的个人信息尚未设置,请设置!");
                }
            }