0

我想从 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();

有谁能够帮助我...

4

2 回答 2

0

与其以这种方式工作,为什么不只Entity Framework用于更新值和XDocumentxml 解析呢?

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

请参阅下面的参考:

1.)实体框架

2.) XDocument

于 2013-04-24T01:51:05.123 回答
0

如果元素不存在,则 SelectSingleNode 返回 null,因此当您在 null 上调用 InnerText 时,它会引发异常,因此您可以像这样更改代码:

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

其余代码也是如此,确保元素存在

希望这对你有帮助

于 2013-04-24T01:59:41.450 回答