2010-06-07 41 views
0

這裏是我的xml文件的一部分保存XML直接向數據庫與C#

<teams> 
    <team-profile> 
    <name>Australia</name> 
    <id>1</id> 
    <stats type="Test"> 
     <span>1877-2010</span> 
     <matches>721</matches> 
     <won>339</won> 
     <lost>186</lost> 
     <tied>2</tied> 
     <draw>194</draw> 
     <percentage>47.01</percentage> 
    </stats> 
    <squad> 
     <player id="135" fullname="Shane Warne"/> 
     <player id="136" fullname="Damien Martyn"/> 
     <player id="138" fullname="Michael Clarke"/> 
    </squad> 
    </team-profile> 
</team> 

我已經讀的地方,有一種方法可以直接保存此XML數據庫。我正在使用VS2010。我已經創建了數據集,我需要從這個XML中獲取數據。有什麼方法可以直接在數據集上映射這個XML嗎? 有沒有其他想法? 我還必須將一些其他更復雜的XML文件保存到數據庫。

我試過xsd.exe來爲這個XML創建xsd模式。

+0

你是什麼意思?你想要一個單元格保存原始的XML文本? – SLaks 2010-06-07 17:56:36

回答

2

的SQL Server 2005+有一個原生XML數據類型,如果你創建一個列,你可以簡單地做一個插入到該列,這裏是一個insert與你相似,該表的第二列的數據類型是XML

INSERT INTO docs VALUES (1, '<book genre="security" 
publicationdate="2002" ISBN="0-7356-1588-2"> 
    <title>Writing Secure Code</title> 
    <author> 
     <first-name>Michael</first-name> 
     <last-name>Howard</last-name> 
    </author> 
    <author> 
     <first-name>David</first-name> 
     <last-name>LeBlanc</last-name> 
    </author> 
    <price>39.99</price> 
</book>') 
INSERT INTO docs VALUES (2, 
'<doc id="123"> 
    <sections> 
    <section num="1"><title>XML Schema</title></section> 
    <section num="3"><title>Benefits</title></section> 
    <section num="4"><title>Features</title></section> 
    </sections> 
</doc>') 
+0

另外值得注意的是,由於您提到了數據集,因此數據集具有writeXml屬性,您可以使用它獲取基礎xml,並通過中間內存流將其保存,如果您不想將其寫入文件。 – kd7 2010-06-07 18:10:54

+0

謝謝,我要試一試。 – SMUsamaShah 2010-06-07 18:30:03

+0

我的意思是writexml方法很抱歉。 – kd7 2010-06-07 18:48:35