从XML文件读取到Sql server表C#

时间:2013-04-24 01:46:14

标签: c# sql-server xml

我想从XML文件读取数据到sql server表,如果所有nod都存在但我的代码正常工作但是如果其中一个节点不存在它会显示错误 这是我的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 table

          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();

任何人都可以帮助我......

2 个答案:

答案 0 :(得分:0)

为什么不使用Entity Framework来更新值,而XDocument使用xml Parsing,而不是那样工作?

string 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>";

//use var xDoc = XDocument.Load(xmlPathGoesHere); if you using xml path
var xDoc = XDocument.Parse(xml);
var studentElements = xDoc.Descendants("student");

studentElements.ToList().ForEach(student => {
    //Create you Insert Query here, see sample below how to get values
    var rfid = student.Element("rfidip").Value;
    var timeIn = student.Element("timegetin").Value;
    var timeOut= string.Empty;
    try {
        timeOut = student.Element("timegetout").Value;
    } catch {
        //since you have no xml value on your time get out, I catch this line
    }
});

见下面的refence:

1。)Entity Framework

2。)XDocument

答案 1 :(得分:0)

如果元素不存在,SelectSingleNode将返回null, 因此,当您在null上调用InnerText时,它会抛出异常 所以你可以改变你的代码:

rfidip = (node.SelectSingleNode("rfidip") != null)? node.SelectSingleNode("rfidip").InnerText.ToString(): string.Empty; 

同样适用于其余代码,确保元素存在

希望这可以帮助你