2014-04-03 80 views
0

我有這種情況。我的公司希望我創建這個應用程序,該應用程序從我們從其他來源獲得的XML文件中讀取和插入數據。我堅持的部分是,當我收到的XML文件:將數據插入數據庫的C#/ 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> 

我無法將數據插入到我目前的代碼數據庫,除非我拿出MLNS =「」和xmlns =「HTTP ://www.npdb-hipdb.hrsa.gov/QueryResponseCommon「從XML文件手動。我覺得像服用此信息出來的文件是錯誤的,但下面是我的代碼:

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)。任何幫助,將不勝感激。

回答

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

     } 
    }