读书人

小弟我有一端关于DOM解析xml的代码老

发布时间: 2012-03-08 13:30:13 作者: rapoo

我有一端关于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。。
有高手帮我看看了。谢谢

[解决办法]


详细的错误信息是什么?



[解决办法]
你可以用XPath来做
[解决办法]
哪行代码出错了??
[解决办法]
调试一下吧
[解决办法]
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);
}
[解决办法]
上面的代码只是告诉你XPath怎么用
具体要是转换成int或者DateTime的话


最好先得到InnerText然后用int.TryParse和DateTime.TryParse转换


[解决办法]
也就是说
<?xml version= "1.0 "?>
<!--system information-->
<setting>
都是ChildNode

取name应该这样
name = xml_doc.ChildNodes[2].ChildNodes[0].ChildNodes[0].InnerText;
你可以测试看一下
[解决办法]
而且在Convert.ToXXX的时候最好判断一下数据
是否可以转换
否则会抛异常
因此最好用TryParse

TryParse的语法可以参考MSDN
[解决办法]
up
[解决办法]
up
[解决办法]
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( "你的个人信息尚未设置,请设置! ");
}
}
[解决办法]
up

读书人网 >C#

热点推荐