我想从 XML 文件读取数据到 sql server 表,如果所有节点都存在,我的代码可以正常工作,但如果其中一个节点不存在,它会显示错误这里是我的 xml 文件
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<student>
<rfidip>3000E2009150501900880530DE07</rfidip>
<timegetin>2013-04-09T00:53:25+03:00</timegetin>
<timegetout>2013-04-09T00:55:59+03:00</timegetout>
</student>
<student>
<rfidip>3000E20075232318015114907CF6</rfidip>
<timegetin>2013-04-09T00:53:25+03:00</timegetin>
<timegetout>2013-04-09T00:55:59+03:00</timegetout>
</student>
<student>
<rfidip>3000E20075232318015112409741</rfidip>
<timegetin>2013-04-09T00:53:25+03:00</timegetin>
</student>
</DocumentElement>
这是我读取 XML 并将数据插入 sql server 表的代码
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:\\Users\\TOSHIBA\\Desktop\\student.XML");
XmlNodeList dataNodes = xmlDoc.SelectNodes("/DocumentElement/student");
foreach (XmlNode node in dataNodes)
{
rfidip = node.SelectSingleNode("rfidip").InnerText.ToString();
timegetin = (node.SelectSingleNode("timegetin").InnerText);
timegetout = node.SelectSingleNode("timegetout").InnerText;
sql = "insert into tripStudent (Student_ID,trip_number,time_getin,time_getout) values(@rfidip,@trip_number,@timegetin,@timegetout)";
//sql = "insert into students values(" + rfidip + ",'" + timegetin + "'," + timegetout + ")";
command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@rfidip", rfidip);
command.Parameters.AddWithValue("@trip_number",trip_number);
command.Parameters.AddWithValue("@timegetin", Convert.ToDateTime(timegetin));
command.Parameters.AddWithValue("@timegetout", Convert.ToDateTime(timegetout));
command.ExecuteNonQuery();
有谁能够帮助我...