使用c#(3.5)将复杂的xml信息插入sql数据库

时间:2014-03-21 13:52:51

标签: c# sql sql-server xml winforms

我一直在网上搜索,找不到直截了当的答案。我正在使用c#(Visual Studio 2005)和xml开始这个项目。我想在我的sql数据库中的这个复杂的xml文件中插入信息。这是我的代码:

这是我正在阅读的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<queryConfirmation>
    <submitter>
        <entityDBID>800000000000001</entityDBID>
        <vendorID>1111514</vendorID>
    </submitter>
    <submissionFilename>test.xml</submissionFilename>
    <certification>
        <name>JOE SMITH</name>
        <title>SUPERVISOR</title>
        <phone>
            <number>1234567890</number>
            <extension>11</extension>
        </phone>
        <date>2008-02-26</date>
    </certification>
    <batchStatus>
        <dcn>2000000000387584</dcn>
        <processDate>2014-03-13</processDate>
        <successfullyProcessed>false</successfullyProcessed>
        <error>
            <code>09</code>
            <message>09: This entity does not have the privilege to perform this transaction.</message>
        </error>
    </batchStatus>
</queryConfirmation>

到目前为止,这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Data.SqlClient;

namespace QRXSprocessPrototype2
{
    public partial class Form1 : Form
    {
        SqlConnection DRDB = new SqlConnection("Data Source=MA_DEV1;Initial Catalog=QRXS;Integrated Security=True");
        SqlDataAdapter DA = new SqlDataAdapter();
        DataSet DS = new DataSet("response");

        public string entityDBID;
        public string vendorID;
        public string submissionFilename;
        public string name;
        public string title;
        public string phoneNumber;
        public string phoneExtension;
        DateTime certificationDate;
        public string dcn;
        DateTime processDate;
        public string successfullyProcessed;
        public string code;
        public string message;

        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\QRXS\download\14000000000000251943.xml");

            XmlElement xelRoot = doc.DocumentElement;
            XmlNodeList submitterNodes = xelRoot.SelectNodes("/queryConfirmation/submitter");
            XmlNodeList subFileNameNode = xelRoot.SelectNodes("/queryConfirmation");
            XmlNodeList certNode = xelRoot.SelectNodes("/queryConfirmation/certification");
            XmlNodeList certPhoneNode = xelRoot.SelectNodes("/queryConfirmation/certification/phone");
            XmlNodeList batchNodeList = xelRoot.SelectNodes("/queryConfirmation/batchStatus");
            XmlNodeList bsErrorList = xelRoot.SelectNodes("/queryConfirmation/error");

            DRDB.Open();

            foreach (XmlNode xndNode in submitterNodes)
        {
             entityDBID = xndNode["entityDBID"].InnerText;
             vendorID = xndNode["vendorID"].InnerText;


             foreach (XmlNode submisFilenameNode in rootNodes)
            {
                submissionFilename = submisFilenameNode["submissionFilename"].InnerText;
                //I get a nullreferenceexception on this line
            }

            foreach (XmlNode cfn in certNode)
            {
                name = cfn["name"].InnerText;
                title = cfn["title"].InnerText;
                certificationDate = DateTime.Parse(cfn["date"].Value.ToString());
            }

            foreach (XmlNode cfnp in certPhoneNode)
            {
                phoneNumber = cfnp["number"].Value;
                phoneExtension = cfnp["extension"].Value;
            }

            foreach (XmlNode bsNode in batchNodeList)
            {
                 dcn = xndNode["dcn"].InnerText;
                 processDate = DateTime.Parse(bsNode["processDate"].Value.ToString());
                 successfullyProcessed = bsNode["successfullyProcessed"].InnerText;
            }

            foreach (XmlNode bsError in bsErrorList)
            {
                code = bsError["code"].Value;
                message = bsError["message"].InnerText;
            }

                DA.InsertCommand = new SqlCommand("INSERT INTO response VALUES (" + entityDBID + ",'" + vendorID + "'," + submissionFilename + "'," + name + "'," + title + "'," + phoneNumber + "'," + phoneExtension + "'." + certificationDate + "'." + dcn + "'." + processDate + "'." + successfullyProcessed + "'." + code + "'." + message + ")", DRDB);
            }

            DRDB.Close();
        }
    }
}

这不是代码的最终形式。我只是想知道在for循环中哪里出错,将信息插入数据库。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

哦,男人,我觉得自己是一个白痴因为没有给我的代码一次。我离项目太近了。

private void button3_Click(object sender, EventArgs e)
    {



        XmlDocument doc = new XmlDocument();
        doc.Load(@"C:\QRXS\download\14000000000000251943.xml");

        XmlElement xelRoot = doc.DocumentElement;
        XmlNodeList rootNodes = xelRoot.SelectNodes("/queryConfirmation");
        XmlNodeList submitterNodes = xelRoot.SelectNodes("/queryConfirmation/submitter"); 
        XmlNodeList certNode = xelRoot.SelectNodes("/queryConfirmation/certification");
        XmlNodeList certPhoneNode = xelRoot.SelectNodes("/queryConfirmation/certification/phone");
        XmlNodeList batchNodeList = xelRoot.SelectNodes("/queryConfirmation/batchStatus");
        XmlNodeList bsErrorList = xelRoot.SelectNodes("/queryConfirmation/batchStatus/error");

        foreach (XmlNode xndNode in submitterNodes)
        {
            entityDBID = xndNode["entityDBID"].InnerText;
            vendorID = xndNode["vendorID"].InnerText;


            foreach (XmlNode submisFilenameNode in rootNodes)
            {
                submissionFilename = submisFilenameNode["submissionFilename"].InnerText;

            }

            foreach (XmlNode cfn in certNode)
            {
                name = cfn["name"].InnerText;
                title = cfn["title"].InnerText;
                certificationDate = cfn["date"].InnerText;
            }

            foreach (XmlNode cfnp in certPhoneNode)
            {
                phoneNumber = cfnp["number"].InnerText;
                phoneExtension = cfnp["extension"].InnerText;
            }

            foreach (XmlNode bsNode in batchNodeList)
            {
                dcn = bsNode["dcn"].InnerText;
                processDate = bsNode["processDate"].InnerText;
                successfullyProcessed = bsNode["successfullyProcessed"].InnerText;
            }

            foreach (XmlNode bsError in bsErrorList)
            {
                code = bsError["code"].InnerText;
                message = bsError["message"].InnerText;
            }
            try
            {
                DA.InsertCommand = new SqlCommand("INSERT INTO response VALUES (@entityDBID, @vendorID, @submissionFilename, @fullName, @title, @number, @extension, @certificationDate, @dcn, @processDate, @successfullyProcessed, @code, @message);", DRDB);
                DA.InsertCommand.Parameters.AddWithValue("@entityDBID", SqlDbType.NVarChar).Value = entityDBID;
                DA.InsertCommand.Parameters.AddWithValue("@vendorID", SqlDbType.NVarChar).Value = vendorID;
                DA.InsertCommand.Parameters.AddWithValue("@submissionFilename", SqlDbType.NVarChar).Value = submissionFilename;
                DA.InsertCommand.Parameters.AddWithValue("@fullName", SqlDbType.NVarChar).Value = name;
                DA.InsertCommand.Parameters.AddWithValue("@title", SqlDbType.NVarChar).Value = title;
                DA.InsertCommand.Parameters.AddWithValue("@number", SqlDbType.NVarChar).Value = phoneNumber;
                DA.InsertCommand.Parameters.AddWithValue("@extension", SqlDbType.NVarChar).Value = phoneExtension;
                DA.InsertCommand.Parameters.AddWithValue("@certificationDate", SqlDbType.NVarChar).Value = certificationDate;
                DA.InsertCommand.Parameters.AddWithValue("@dcn", SqlDbType.NVarChar).Value = dcn;
                DA.InsertCommand.Parameters.AddWithValue("@processDate", SqlDbType.NVarChar).Value = processDate;
                DA.InsertCommand.Parameters.AddWithValue("@successfullyProcessed", SqlDbType.NVarChar).Value = successfullyProcessed;
                DA.InsertCommand.Parameters.AddWithValue("@code", SqlDbType.NVarChar).Value = code;
                DA.InsertCommand.Parameters.AddWithValue("@message", SqlDbType.NVarChar).Value = message;

                DRDB.Open();
                DA.InsertCommand.ExecuteNonQuery();
                DRDB.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }



    }