用于将数据插入数据库的C#/ XML问题

时间:2014-04-03 16:53:31

标签: c# xml ado.net visual-studio-2005

我有这种情况。我的公司希望我创建这个应用程序,从我们从其他来源获取的XML文件中读取和插入数据。我坚持的部分是当我收到XML文件时:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<queryConfirmation xmlns="http://www.npdb-hipdb.hrsa.gov/QueryResponseCommon">
<submitter xmlns="">
    <entityDBID>800000000000001</entityDBID>
    <vendorID>1111514</vendorID>
</submitter>
<submissionFilename xmlns="">test.xml</submissionFilename>
<certification xmlns="">
    <name>JOE SMITH</name>
    <title>SUPERVISOR</title>
    <phone>
        <number>4075556647</number>
    </phone>
    <date>2008-02-26</date>
</certification>
<batchStatus xmlns="">
    <dcn>2000000000388120</dcn>
    <processDate>2014-04-03</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>

除非我从XML文件中取出mlns =“”和xmlns =“http://www.npdb-hipdb.hrsa.gov/QueryResponseCommon”,否则我无法使用当前代码将数据插入数据库手动。我觉得从文件中取出这些信息是错误的,但以下是我的代码:

private void insertResponseXml()
    {

        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "XML|*.xml";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(ofd.FileName);




            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;

                        if (cfnp["extension"] == null)
                        {
                            phoneExtension = "null";
                        }
                        else
                        {
                            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)
                    {
                        if (bsError["code"] == null)
                        {
                            code = "null";
                        }
                        else
                        {
                            code = bsError["code"].InnerText;
                        }

                        if (bsError["message"] == null)
                        {
                            message = "null";
                        }
                        else
                        {
                            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);
                    }

                }


        }
    }

另外更糟糕的是我正在使用Visual Studio 2005(C#2.5)。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我刚刚更改了代码

private void readAndInsertXmlFile()
    {
        OpenFileDialog openFile = new OpenFileDialog();
        openFile.Filter = "XML|*.xml";
        if (openFile.ShowDialog() == DialogResult.OK)
          {

            using (XmlTextReader reader = new XmlTextReader(openFile.FileName))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        switch (reader.Name)
                        {
                            case "entityDBID":


                                    qc.submitter.entityDBID = reader.ReadString();


                                break;
                            case "vendorID":

                                    qc.submitter.vendorID = reader.ReadString();


                                break;
                            case "submissionFilename":

                                    qc.submissionFilename.SubmissionFilename = reader.ReadString();


                                break;
                            case "name":

                                    qc.certification.name = reader.ReadString();


                                break;
                            case "title":

                                    qc.certification.title = reader.ReadString();


                                break;
                            case "number":

                                    qc.certification.phone.number = reader.ReadString();


                                break;
                            case "extension":


                                    qc.certification.phone.extension = reader.ReadString();



                                break;
                            case "date":

                                    qc.certification.date = reader.ReadString();


                                break;
                            case "dcn":

                                    qc.batchStatus.dcn = reader.ReadString();


                                break;
                            case "processDate":

                                    qc.batchStatus.processDate = reader.ReadString();


                                break;
                            case "successfullyProcessed":

                                    qc.batchStatus.successfullyProcessed = reader.ReadString();


                                break;
                            case "code":

                                    qc.error.Code = reader.ReadString();


                                break;
                            case "message":

                                    qc.error.Message = reader.ReadString();


                                break;



                        }


                    }

                }
                DA.InsertCommand = new SqlCommand("INSERT INTO response VALUES (@entityDBID, @vendorID, @submissionFilename, @fullName, @title, @number, @extension, @certificationDate, @dcn, @processDate, @successfullyProcessed, @code, @message);", DRDB);
                if (qc.submitter.entityDBID == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@entityDBID", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@entityDBID", SqlDbType.NVarChar).Value = qc.submitter.entityDBID;
                }

                if (qc.submitter.vendorID == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@vendorID", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@vendorID", SqlDbType.NVarChar).Value = qc.submitter.vendorID;
                }

                if (qc.submissionFilename.SubmissionFilename == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@submissionFilename", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@submissionFilename", SqlDbType.NVarChar).Value = qc.submissionFilename.SubmissionFilename;
                }

                if (qc.certification.name == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@fullName", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@fullName", SqlDbType.NVarChar).Value = qc.certification.name;
                }

                if (qc.certification.title == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@title", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@title", SqlDbType.NVarChar).Value = qc.certification.title;
                }

                if (qc.certification.phone.number == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@number", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@number", SqlDbType.NVarChar).Value = qc.certification.phone.number;
                }

                if (qc.certification.phone.extension == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@extension", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@extension", SqlDbType.NVarChar).Value = qc.certification.phone.extension;
                }


                if (qc.certification.date == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@certificationDate", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@certificationDate", SqlDbType.NVarChar).Value = qc.certification.date;
                }

                if (qc.batchStatus.dcn == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@dcn", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@dcn", SqlDbType.NVarChar).Value = qc.batchStatus.dcn;
                }

                if (qc.batchStatus.processDate == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@processDate", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@processDate", SqlDbType.NVarChar).Value = qc.batchStatus.processDate;
                }

                if (qc.batchStatus.successfullyProcessed == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@successfullyProcessed", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@successfullyProcessed", SqlDbType.NVarChar).Value = qc.batchStatus.successfullyProcessed;
                }

                if (qc.error.Code == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@code", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@code", SqlDbType.NVarChar).Value = qc.error.Code;
                }

                if (qc.error.Message == null)
                {
                    DA.InsertCommand.Parameters.AddWithValue("@message", SqlDbType.NVarChar).Value = "NULL";
                }
                else
                {
                    DA.InsertCommand.Parameters.AddWithValue("@message", SqlDbType.NVarChar).Value = qc.error.Message;
                }

                DRDB.Open();
                DA.InsertCommand.ExecuteNonQuery();
                DRDB.Close();
            }

        }
    }