2016-09-23 8 views
0

XmlFileをSql-Serverにロードしようとしていますが、sqlbulkcopyを使用しますが、XmlFileをデータセットに変換する方法がわかりません。 。ここに私のXmlFileとありがとうございます。XmlFileをSql-serverにロードする

<?xml version="1.0" encoding="utf-8"?> 
<data> 
<data> 
<created_time>2016-09-23T22:37:33+01:00</created_time> 
<id>12451</id>  
<message>Cool</message> 
<from> 
    <id>1</id> 
    <name>user1</name> 
    <picture> 
    <data> 
     <is_silhouette>false</is_silhouette> 
     <url>https://server.com/pic.jpg</url> 
    </data> 
    </picture> 
</from> 
<comments> 
    <data> 
    <created_time>2016-09-23T22:46:59+01:00</created_time>  
    <id>112</id>   
    <message>ok</message> 
    <from> 
     <id>1245</id> 
     <name>reply user1</name> 
     <picture> 
     <data> 
      <is_silhouette>false</is_silhouette> 
      <url>https://server.com/pic2.jpg</url> 
     </data> 
     </picture> 
    </from> 
    </data> 
    <data> 
    <created_time>2016-09-23T22:47:41+01:00</created_time>  
    <id>113</id>   
    <message>nice</message> 
    <from> 
     <id>1246</id> 
     <name>reply user2</name> 
     <picture> 
     <data> 
      <is_silhouette>false</is_silhouette> 
      <url>https://server.com/pic3.jpg</url> 
     </data> 
     </picture> 
    </from> 
    </data>  
    <paging> 
    <cursors> 
     <before>sdfdfdsfdfdsfdsf</before> 
     <after>dsfdsfdfsfdffdfdf</after> 
    </cursors> 
    </paging> 
</comments> 
</data> 
<data> 
<created_time>2016-09-23T22:35:20+01:00</created_time>  
<id>47854</id>  
<message>Thank you</message> 
<from> 
    <id>365</id> 
    <name>user2</name> 
    <picture> 
    <data> 
     <is_silhouette>false</is_silhouette> 
     <url>https://server.com/pic4.jpg</url> 
    </data> 
    </picture> 
</from> 
<comments>  
    <data> 
    <created_time>2016-09-23T22:47:05+01:00</created_time> 
    <id>1285</id>   
    <message>what?</message> 
    <from> 
     <id>33</id> 
     <name>reply user1</name> 
     <picture> 
     <data> 
      <is_silhouette>false</is_silhouette> 
      <url>https://server.com/pic5.jpg</url> 
     </data> 
     </picture> 
    </from> 
    </data> 
    <data> 
    <created_time>2016-09-23T22:55:54+01:00</created_time> 
    <id>1982</id>   
    <message>No.</message> 
    <from> 
     <id>102</id> 
     <name>reply user2</name> 
     <picture> 
     <data> 
      <is_silhouette>false</is_silhouette> 
      <url>https://server.com/pic6.jpg</url> 
     </data> 
     </picture> 
    </from> 
    </data>  
    </comments> 
</data>  
</data> 
+0

http://stackoverflow.com/q/3491347/6741942の質問に記載されているように、単純なアプローチ(ds.ReadXml(myXMLfile);)を試したことがありますか?DataSetが自動的にスキーマを決定すると思います。 – strongbutgood

+0

(ds.ReadXml(myXMLfile))を使用しています;「同じテーブル(データ)が2つのネストされたリレーション内の子テーブルになることはできません」エラーを返します。 – hansly

答えて

0

最後に解決策はMS-SQLサーバーにXmlFileを読み込むことです。

cmd.CommandText = "Insert into Table_fileXml(xml) SELECT CONVERT(XML, BulkColumn) AS BulkColumn From OPENROWSET(BULK 'd:\\XmlFile.xml', SINGLE_BLOB) AS X"; 

cmd.ExecuteNonQuery(); 

cmd.Parameters.Clear(); 

そして最後に、私のファイルXMLFILEが私のMS-SQL Serverデータベースに挿入されることの後に、ストアドプロシージャを実行しますトリガーの作成:

USE [DatabaseName] 
GO 

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER TRIGGER [dbo].[trigger1] 
ON [dbo].[Table_FileXml] 
AFTER INSERT 
AS 
BEGIN 
-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
SET NOCOUNT ON; 

-- Insert statements for trigger here 

DECLARE @xml xml 
SELECT top(1) @xml=xml FROM Table_fileXml ORDER BY id DESC 

SELECT data.value('created_time[1]','datetime') AS Data_CreatedTime 
    ,data.value('id[1]','nvarchar(max)') AS Data_Id 
    ,data.value('message[1]','nvarchar(max)') AS Data_Message 
    ,data.value('(from/id)[1]','nvarchar(50)') AS Data_From_Id 
    --more values here 
    ,data.value('(from/picture/data/url)[1]','nvarchar(max)') AS Data_From_Picture_URL 
    --more values here 

    ,comment.value('created_time[1]','datetime') AS Comment_CreatedTime 
    ,comment.value('id[1]','nvarchar(max)') AS Comment_Id 
    ,comment.value('message[1]','nvarchar(max)') AS Comment_Message 
    ,comment.value('(from/id)[1]','nvarchar(50)') AS Comment_From_Id 
    --more values here 
    ,comment.value('(from/picture/data/url)[1]','nvarchar(max)') AS Comment_From_Picture_URL 
    --more values here 

FROM @xml.nodes('/data/data') AS A(data) 
OUTER APPLY data.nodes('comments/data') AS B(comment) 
BEGIN 
DECLARE @xml2 xml 
SELECT top(1) @xml2=xml FROM Table_fileXml ORDER BY id DESC 
INSERT INTO Table_comment(from_id,from_pic,comment_message) 
SELECT DISTINCT comment.value('(from/id)[1]','nvarchar(50)') AS from_id, 
data.value('(from/picture/data/url)[1]','nvarchar(max)') AS from_pic, 
comment.value('message[1]','nvarchar(max)') AS comment_message 
FROM @xml2.nodes('/data/data') AS A(data) 
OUTER APPLY data.nodes('comments/data') AS B(comment) 
END 
END 

を私のC#のプログラムでは、私は、このコマンドを使用しますそして結果は正しい。ストアドプロシージャを使用せずにトリガを使用するだけで、私のFileXMLの最後の行が得られます。そのため、私はトリガでストアドプロシージャを使用する必要があります。

貴重なご協力をいただき、ありがとうございました。 "Shnugo"、 "Martin Staufcik"、 "Abouzar"

0

あなたはあなたの中にbyte[]プロパティにマップされますタイプvarbinary(max)のデータベース列を作成することができますドメインモデル。その後、XmlWriterを使用してXML文書をこの列に保管します。

public virtual void Save(
    XmlWriter w 
) 
+0

Xmlファイルを細断して属性と要素を別々の列に格納したい"データ "を1つのテーブルに、"コメント "を2番目のテーブルに追加 – hansly

+0

おそらく問題は要素' data'がルートレベルとコメントノードの内側にも名前を変更することができます。 –

0

次のクエリを実行すると、派生したテーブル内のXMLからすべてのデータを取得できます。

これを適切な表に挿入するのは簡単です。ヒント:

SELECT DISTINCT ... 
SELECT col1, col2 INTO #tbl ... 
INSERT INTO tbl SELECT col1, col2, ... 

そして、あなたは、フィッティングPARTITION BYROW_NUMBER() OVER()のようなものを使用することができますIDの作成のための...

あなたのXML

DECLARE @xml XML= 
'<?xml version="1.0" encoding="utf-8"?> 
<data> 
<data> 
<created_time>2016-09-23T22:37:33+01:00</created_time> 
<id>12451</id>  
<message>Cool</message> 
<from> 
    <id>1</id> 
    <name>user1</name> 
    <picture> 
    <data> 
     <is_silhouette>false</is_silhouette> 
     <url>https://server.com/pic.jpg</url> 
    </data> 
    </picture> 
</from> 
<comments> 
    <data> 
    <created_time>2016-09-23T22:46:59+01:00</created_time>  
    <id>112</id>   
    <message>ok</message> 
    <from> 
     <id>1245</id> 
     <name>reply user1</name> 
     <picture> 
     <data> 
      <is_silhouette>false</is_silhouette> 
      <url>https://server.com/pic2.jpg</url> 
     </data> 
     </picture> 
    </from> 
    </data> 
    <data> 
    <created_time>2016-09-23T22:47:41+01:00</created_time>  
    <id>113</id>   
    <message>nice</message> 
    <from> 
     <id>1246</id> 
     <name>reply user2</name> 
     <picture> 
     <data> 
      <is_silhouette>false</is_silhouette> 
      <url>https://server.com/pic3.jpg</url> 
     </data> 
     </picture> 
    </from> 
    </data>  
    <paging> 
    <cursors> 
     <before>sdfdfdsfdfdsfdsf</before> 
     <after>dsfdsfdfsfdffdfdf</after> 
    </cursors> 
    </paging> 
</comments> 
</data> 
<data> 
<created_time>2016-09-23T22:35:20+01:00</created_time>  
<id>47854</id>  
<message>Thank you</message> 
<from> 
    <id>365</id> 
    <name>user2</name> 
    <picture> 
    <data> 
     <is_silhouette>false</is_silhouette> 
     <url>https://server.com/pic4.jpg</url> 
    </data> 
    </picture> 
</from> 
<comments>  
    <data> 
    <created_time>2016-09-23T22:47:05+01:00</created_time> 
    <id>1285</id>   
    <message>what?</message> 
    <from> 
     <id>33</id> 
     <name>reply user1</name> 
     <picture> 
     <data> 
      <is_silhouette>false</is_silhouette> 
      <url>https://server.com/pic5.jpg</url> 
     </data> 
     </picture> 
    </from> 
    </data> 
    <data> 
    <created_time>2016-09-23T22:55:54+01:00</created_time> 
    <id>1982</id>   
    <message>No.</message> 
    <from> 
     <id>102</id> 
     <name>reply user2</name> 
     <picture> 
     <data> 
      <is_silhouette>false</is_silhouette> 
      <url>https://server.com/pic6.jpg</url> 
     </data> 
     </picture> 
    </from> 
    </data>  
    </comments> 
</data>  
</data>'; 

クエリ

SELECT data.value('created_time[1]','datetime') AS Data_CreatedTime 
     ,data.value('id[1]','int') AS Data_Id 
     ,data.value('message[1]','nvarchar(max)') AS Data_Message 
     ,data.value('(from/id)[1]','int') AS Data_From_Id 
     --more values here 
     ,data.value('(from/picture/data/url)[1]','nvarchar(max)') AS Data_From_Picture_URL 
     --more values here 

     ,comment.value('created_time[1]','datetime') AS Comment_CreatedTime 
     ,comment.value('id[1]','int') AS Comment_Id 
     ,comment.value('message[1]','nvarchar(max)') AS Comment_Message 
     ,comment.value('(from/id)[1]','int') AS Comment_From_Id 
     --more values here 
     ,comment.value('(from/picture/data/url)[1]','nvarchar(max)') AS Comment_From_Picture_URL 
     --more values here 
FROM @xml.nodes('/data/data') AS A(data) 
OUTER APPLY data.nodes('comments/data') AS B(comment) 

結果

+-------------------------+---------+--------------+--------------+-----------------------------+-------------------------+------------+-----------------+-----------------+-----------------------------+ 
| Data_CreatedTime  | Data_Id | Data_Message | Data_From_Id | Data_From_Picture_URL  | Comment_CreatedTime  | Comment_Id | Comment_Message | Comment_From_Id | Comment_From_Picture_URL | 
+-------------------------+---------+--------------+--------------+-----------------------------+-------------------------+------------+-----------------+-----------------+-----------------------------+ 
| 2016-09-23 21:37:33.000 | 12451 | Cool   | 1   | https://server.com/pic.jpg | 2016-09-23 21:46:59.000 | 112  | ok    | 1245   | https://server.com/pic2.jpg | 
+-------------------------+---------+--------------+--------------+-----------------------------+-------------------------+------------+-----------------+-----------------+-----------------------------+ 
| 2016-09-23 21:37:33.000 | 12451 | Cool   | 1   | https://server.com/pic.jpg | 2016-09-23 21:47:41.000 | 113  | nice   | 1246   | https://server.com/pic3.jpg | 
+-------------------------+---------+--------------+--------------+-----------------------------+-------------------------+------------+-----------------+-----------------+-----------------------------+ 
| 2016-09-23 21:35:20.000 | 47854 | Thank you | 365   | https://server.com/pic4.jpg | 2016-09-23 21:47:05.000 | 1285  | what?   | 33    | https://server.com/pic5.jpg | 
+-------------------------+---------+--------------+--------------+-----------------------------+-------------------------+------------+-----------------+-----------------+-----------------------------+ 
| 2016-09-23 21:35:20.000 | 47854 | Thank you | 365   | https://server.com/pic4.jpg | 2016-09-23 21:55:54.000 | 1982  | No.    | 102    | https://server.com/pic6.jpg | 
+-------------------------+---------+--------------+--------------+-----------------------------+-------------------------+------------+-----------------+-----------------+-----------------------------+ 
関連する問題