我有一端关于DOM解析xml的代码,,老是出错.写出来大家给我看看..谢谢!

sunote 2007-08-27 02:09:43
做一个关于日程安排的小东西。。新手
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。。
有高手帮我看看了。谢谢
...全文
166 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
YunAo2008 2007-08-27
  • 打赏
  • 举报
回复
up
zhobin 2007-08-27
  • 打赏
  • 举报
回复
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("你的个人信息尚未设置,请设置!");
}
}
Radar2006 2007-08-27
  • 打赏
  • 举报
回复
up
sunote 2007-08-27
  • 打赏
  • 举报
回复

我再试一下。。谢谢大家
zhchg6666 2007-08-27
  • 打赏
  • 举报
回复
up
lovefootball 2007-08-27
  • 打赏
  • 举报
回复
而且在Convert.ToXXX的时候最好判断一下数据
是否可以转换
否则会抛异常
因此最好用TryParse

TryParse的语法可以参考MSDN
lovefootball 2007-08-27
  • 打赏
  • 举报
回复
也就是说
<?xml version="1.0"?>
<!--system information-->
<setting>
都是ChildNode

取name应该这样
name = xml_doc.ChildNodes[2].ChildNodes[0].ChildNodes[0].InnerText;
你可以测试看一下
lovefootball 2007-08-27
  • 打赏
  • 举报
回复
你可以跟踪调试一下或者参考MSDN,看看ChildNodes是什么概念
不是你理解的那样~~~~
lovefootball 2007-08-27
  • 打赏
  • 举报
回复
上面的代码只是告诉你XPath怎么用
具体要是转换成int或者DateTime的话
最好先得到InnerText然后用int.TryParse和DateTime.TryParse转换

lovefootball 2007-08-27
  • 打赏
  • 举报
回复
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);
}
xjjdanran 2007-08-27
  • 打赏
  • 举报
回复
调试一下吧
wzd24 2007-08-27
  • 打赏
  • 举报
回复
哪行代码出错了??
lovefootball 2007-08-27
  • 打赏
  • 举报
回复
你可以用XPath来做
wuyi8808 2007-08-27
  • 打赏
  • 举报
回复


详细的错误信息是什么?


110,533

社区成员

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

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

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