我有一端关于DOM解析xml的代码,,老是出错.写出来大家给我看看..谢谢!
做一个关于日程安排的小东西。。新手
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。。
有高手帮我看看了。谢谢