SqlServer²Ù×÷XMLѧϰ
ÔÚ SQLServer2005 ÖÐ¶Ô XML µÄ´¦Àí¹¦ÄÜÏÔÈ»ÔöÇ¿Á˺ܶ࣬ÌṩÁË query(),value(),exist(),modify(),nodes() µÈº¯Êý¡£
¹ØÓÚ xml £¬ÄÑÒÔÀí½âµÄ²»ÊÇ SQLServer ÌṩµÄº¯Êý£¬¶øÊÇ¶Ô xml ±¾ÉíµÄÀí½â£¬¿´Ëƺܼòµ¥µÄÎļþ¸ñʽ£¬´¦ÀíÆðÀ´È´ÊǷdz£À§Äѵġ£±¾ÎÄÖ»Êdzõ̽һ϶øÒÑ¡£
Ïê¼û SQLServer Áª»ú°ïÖú£º
Ö÷Ìâ
˵Ã÷
query() ·½·¨£¨ xml Êý¾ÝÀàÐÍ£©
´Ë·½·¨ÓÃÓÚ¶Ô XML ʵÀý½øÐвéѯ¡£
value() ·½·¨£¨ xml Êý¾ÝÀàÐÍ£©
´Ë·½·¨ÓÃÓÚ´Ó XML ʵÀý¼ìË÷ SQL ÀàÐ͵ÄÖµ¡£
exist() ·½·¨£¨ xml Êý¾ÝÀàÐÍ£©
´Ë·½·¨ÓÃÓÚÈ·¶¨²éѯÊÇ·ñ·µ»Ø·Ç¿Õ½á¹û¡£
modify() ·½·¨£¨ xml Êý¾ÝÀàÐÍ£©
´Ë·½·¨ÓÃÓÚÖ¸¶¨ XML DML Óï¾äÒÔÖ´ÐиüС£
nodes() ·½·¨£¨ xml Êý¾ÝÀàÐÍ£©
´Ë·½·¨ÓÃÓÚ½« XML ²ð·Ö³É¶àÐÐÒÔ½« XML ÎĵµµÄ×é³É²¿·Ö´«²¥µ½Ðм¯ÖС£
CREATE TABLE VisioXML
(
ID INT,
Doc XML
);
GO
INSERT INTO VisioXML(ID,Doc)
SELECT 4,* from OPENROWSET(BULK 'c:\abc.xml',SINGLE_BLOB) AS x;
--SELECT * from VisioXML WHERE ID=4
SELECT
--Doc.value('(/ROOT/ROW[1]/ID/text())[1]','int') RootRowID1, -- µÚÒ»ÐÐ ID µÄÖµ£¬²¢ÇÒת»»Îª int ÀàÐÍ
--Doc.value('(/ROOT/ROW[2]/ID/text())[1]','int') RootRowID2, -- µÚ¶þÐÐ ID µÄÖµ£¬²¢ÇÒת»»Îª int ÀàÐÍ
--Doc.value('(/ROOT/ROW[1]/NAME/text())[1]','varchar(20)') RootRowNAME1, -- µÚÒ»ÐÐ NAME µÄÖµ£¬²¢ÇÒת»»Îª VARCHAR ÀàÐÍ
--Doc.value('(/ROOT/ROW[1]/NAME/@SEX)[1]','varchar(20)') RootRowNAME1SEX, -- µÚÒ»ÐÐ NAME ÖÐ SEX ÊôÐÔµÄÖµ£¬²¢ÇÒת»»Îª VARCHAR ÀàÐÍ
Doc.query('/ROOT') Root, --ROOT ϵÄËùÓÐ XML ÄÚÈÝ£¬ÀàÐÍΪ XML
--Doc.query('/ROOT/ROW[1]') RootRow1, --ROOT ϵÚÒ»ÐÐËùÓÐµÄ XML ÄÚÈÝ£¬ÀàÐÍΪ XML
Doc.query('/ROOT/ROW[2]') RootRow2 --ROOT ϵڶþÐÐËùÓÐµÄ XML ÄÚÈÝ£¬ÀàÐÍΪ XML
from VisioXML
WHERE ID=4
SELECT
Doc.exist('/ROOT/ROW[1]/NAME[(@SEX cast as xs:string?) = xs:string("MALE")]') Row1EQStringMale,
Doc.exist('/ROOT/ROW[1]/NAME[(@SEX cast as xs:string?) = "MALE"]') Row1EQ
Ïà¹ØÎĵµ£º
<?xml version="1.0" standalone="yes"?>
<imgs>
<pic name="/adv_pic/1.jpg" url="http://www.baidu.com/" title="test" />
<pic name="/adv_pic/2.jpg" url="http://www.baidu.com/" title="test" />
......
public partial class Form1 : Form
{
DataSet ds = new DataSet();
public Form1()
{
......
1. <?xml version="1.0" encoding="GB2312" standalone="yes" ?>
1.1 ÕâÊÇxmlµÄÎĵµÉùÃ÷£¬Ò»°ãλÓÚxmlÎļþµÄÊ×ÐС£
1.2 Êéд¹æ·¶£º<?xmlΪһÌå Ö®¼ä²»ÄÜÓпոñ¡£
1.3 encodingÊôÐÔ£¬Èç¹ ......
·½·¨Ò» £ºÊ¹ÓÃXML¿Ø¼þ
<% @ Page Language="C#"%>
<html>
<body>
<h3><font face="Verdana">¶ÁÈ¡XML·½·¨Ò»</font></h3>
<from runat=server>
<asp:Xml id="xml1" DocumentSource="grade.xml" runat="server" />
</from>
</body>
</ ......
ÍøÉÏÌṩµÄһЩ·½·¨±È½Ï¼òµ¥£¬ÎÞ·¨ÊÊÓ¦XMLÎļþµÄ¶¯Ì¬±ä»¯ £¬Ã»°ì·¨Ö»ÄÜ×Ô¼º²éÎĵµÐ´£¬×ÜËãûÀË·Ñʱ¼ä£¬ÓõÄʱºò±ðÍüÁ˼Ó#import
"msxml4.dll"Õâ¸ö¶¯Ì¬Á´½Ó¿âÍøÉϺܺÃÕÒ£¬×Ô¼ºÕÒһϣ¬Ï£ÍûÄÜ°ïÖúÐèÒªµÄÅóÓÑ£¬À§ÁË
::CoInitialize(NULL);
//³õʼ»¯COM
MSXML2::IXMLDOMDocumentPtr pDoc;
......