2015-10-05 13 views
0

我有一個xml數據,它沒有與它關聯的xslt。它是由我的客戶提供的一個自動生成的xml。下面是xml文檔的示例。使用ssis腳本將沒有XSL的XML數據加載到sql表中任務

HTTP/1.1 200 OK 
 
Content-Length: 47033212 
 
Content-Type: application/xml; charset=utf-8 
 
Server: Microsoft-IIS/7.5 
 
X-Powered-By: ASP.NET 
 
Date: Tue, 22 Sep 2015 23:02:56 GMT 
 

 
<ExportPackageResult xmlns="http://schemas.data.org/2004/07/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><ResponseDateTime>2015-09-22T19:02:44.2363931-04:00</ResponseDateTime> 
 
<datapoints> 
 
<PackageDataPoint> 
 
    <ADDON> 
 
    ..... 
 
    </ADDON> 
 
    <FIELDSET> 
 
    ..... 
 
    </FIELDSET> 
 
</PackageDataPoint>

爲我的表中的數據駐留內的每個PackageDataPoint ../PackageDataPoint而且會有各種的childNodes像ADDON ../ ADDON,FIELDSET ../ FIELDSET等內的各

我正在嘗試使用SSIS腳本任務將addon,fieldset中的每個數據值加載到一個sql表中。我的sql表中的列名將是addon,fieldset等。

下面是我的腳本任務代碼

public void Main() 
     { 
      // TODO: Add your code here 
      System.Data.SqlClient.SqlConnection sqlConn; 
      System.Data.SqlClient.SqlCommand sqlCmd; 

      string strFtpath = Dts.Variables["User::strFTPpath"].Value.ToString(); 
      string strArchivePath = Dts.Variables["User::strArchivePath"].Value.ToString(); 
      string strConnectionStr = Dts.Variables["User::strStagingConnstr"].Value.ToString(); 
      string strFileName = string.Empty; 

      DirectoryInfo di = new DirectoryInfo(strFtpath.ToString()); 
      FileInfo[] getCSVFile = di.GetFiles("*.xml"); 
      foreach (FileInfo fi in getCSVFile) 
      { 
       strFileName = fi.FullName.ToString(); 
      } 
      DataSet dsXml = new DataSet(); 

      XmlDocument readXml = new XmlDocument(); 

      try 
      { 
       readXml.Load(strFileName); 
       using (sqlConn = new SqlConnection(strConnectionStr)); 
       { 
        sqlConn.Open(); 
        sqlCmd = new SqlCommand("usp_XMLtoTable", sqlConn); 
        sqlCmd.CommandType = CommandType.StoredProcedure; 
        sqlCmd.Parameters.Add("@xml", SqlDbType.Xml).Value = dsXml.GetXml(); 
        sqlCmd.ExecuteNonQuery(); 
        sqlConn.Close(); 
       } 

       Dts.TaskResult = (int)ScriptResults.Success; 
       Dts.Variables["User::flgDataLoadStatus"].Value = 1; 
      } 
      catch (Exception e) 
      { 
       Dts.Log(e.Message, 0, null); 
       Dts.TaskResult = (int)ScriptResults.Failure; 
       Dts.Variables["User::flgDataLoadStatus"].Value = 0; 
      } 
     } 

當我跑我得到的錯誤在根級別

數據的代碼無效。行1,位置1.

代表上述代碼。我知道它是因爲我的XML文檔中的前7行,但我不知道如何從XML文檔中刪除它,因爲我不能手動執行它。

我通過使用XMLTextReader,XMLDocument,StreamReader讀取XML文檔嘗試了幾個解決方法,但是我收到了同樣的錯誤「根級數據無效」。

Importing data from XML file to SQL database

Pass XML as Parameter from C# to SQL Stored Procedure in VB.NET

,我使用XML數據保存到SQL表將類似於我的SQL過程這個

CREATE PROCEDURE prc_readxmldata 
(
@XMLdata XML 
) 
AS 
BEGIN 
SELECT 
t.value('(FirstName/text())[1]','nvarchar(120)')AS FirstName , 
t.value('(LastName/text())[1]','nvarchar(120)')AS LastName, 
t.value('(UserName/text())[1]','nvarchar(120)')AS UserName, 
t.value('(Job/text())[1]','nvarchar(120)')AS Job 
FROM 
@XMLdata.nodes('/datapoints/packagedatapoints')AS TempTable(t) 
END 

誰能幫我解決我面臨的錯誤。

回答

0

你正試圖從東西載入XML像「somefilename.xml」

XmlDocument readXml = new XmlDocument(); 

try { 
    readXml.Load(strFileName); 

要先從文件加載XML數據。嘗試這樣的事情。

XmlDocument readXml = new XmlDocument(); 

try { 
    var xmlFileContents = File.ReadAllText(strFileName); 
    readXml.Load(xmlFileContents); 
相關問題