将XML Root值读入表中

时间:2013-09-04 13:09:04

标签: sql sql-server xml sqlxml

我正在处理XML文件以保存到数据库表中。

这是我的XML:

     <?xml version="1.0"encoding="UTF-8"standalone="true"?>
<Computer Name="WH7"Processior="Intel(R) Core(TM)2 Duo CPU     P9600  @ 2.66GHz" Manufacturer="Dell Inc." Model="Latitude E6500 Domain="WORKGROUP" RAM="3536 MB" ServiceTag="DGQKTJ1">
    <BIOS Manufacturer="Dell Inc." Version="Phoenix ROM BIOS PLUS Version 1.10 A19"/>   These properties go into the Asset table
    <OS Name="Microsoft Windows 7 Ultimate" ServicePack="1"/>   OS - Category, Namem, Service Pack - Property, 
    <HardDisks> HardDisks - Category, Value - Save as Name in the Property table
        <HardDisk Value=C:\ (NTFS) Total Space: 127.8373 GB Free Space: 16.2686 GB/>
    </HardDisks>
    <NetworkCards>  NaetworkCards - Category, Adapter, IPAddress, Subnet Mask, MACAddress, DHCPEnable as properties
        <Network Adapter="[00000007] Intel(R) 82567LM Gigabit Network Connection" IPAddress="10.5.99.48" Subnet Mask="255.255.255.0" MACAddress="00:21:70:F7:C9:A2" DHCPEnabled="True"/>
        <Network Adapter="[00000009] VMware Virtual Ethernet Adapter for VMnet1" IPAddress="192.168.216.1" Subnet Mask="255.255.255.0" MACAddress="00:50:56:C0:00:01" DHCPEnabled="False"/>
        <Network Adapter="[00000012] Dell Wireless 1397 WLAN Mini-Card" IPAddress="192.168.1.64" Subnet Mask="255.255.255.0" MACAddress="00:24:2C:63:F5:08" DHCPEnabled="True"/>
        <Network Adapter="[00000013] VMware Virtual Ethernet Adapter for VMnet8" IPAddress="192.168.202.1" Subnet Mask="255.255.255.0" MACAddress="00:50:56:C0:00:08" DHCPEnabled="False"/>
        <Network Adapter="[00000020] Cisco Systems VPN Adapter" IPAddress="172.31.3.251" Subnet Mask="255.255.252.0" MACAddress="00:05:9A:3C:78:00" DHCPEnabled="False"/>
    </NetworkCards>
    <GraphicsCards> GraphicsCards - Category, Card - Property
        <Graphics Card="Current Display Controller Configuration"/>
    </GraphicsCards>
    <LocalGroups>   
        <Group Name="Administrators"> LocalGroups - Category, Name - Property
            <Member="Administrator"/> Member - Category, Administrator, whargrove - Property, Administrators - Parent
            <Member="whargrove"/>
        </Group>
        <Group Name="Backup Operators">
        </Group>
        <Group Name="Cryptographic Operators">
        </Group>
        <Group Name="Distributed COM Users">
        </Group>
        <Group Name="Event Log Readers">
        </Group>
        <Group Name="Guests">
            <Member="Guest"/>
        </Group>
        <Group Name="IIS_IUSRS"> LocalGroups - Category, IIS_USERS - Property
            <Member="IUSR"/> LocalGroups - Member, IUSER - Property, IIS_USERS - Parent
        </Group>
        <Group Name="Network Configuration Operators">
        </Group>
        <Group Name="Performance Log Users">
        </Group>
        <Group Name="Performance Monitor Users">
        </Group>
        <Group Name="Power Users">
        </Group>
        <Group Name="Remote Desktop Users">
        </Group>
        <Group Name="Replicator">
        </Group>
        <Group Name="Users">
            <Member="INTERACTIVE"/>
            <Member="Authenticated Users"/>
            <Member="IPS_admin"/>
        </Group>
        <Group Name="boinc_admins">
            <Member="Administrators"/>
            <Member="whargrove"/>
        </Group>
        <Group Name="boinc_projects">
        </Group>
        <Group Name="boinc_users">
        </Group>
        <Group Name="HomeUsers">
            <Member="whargrove"/>
            <Member="Administrator"/>
            <Member="HomeGroupUser$"/>
        </Group>
        <Group Name="__vmware__">
            <Member="__vmware_user__"/>
        </Group>
    </LocalGroups>
    <SoftwareInstalled> SoftwareInstalled - Category, ProductName, VersionString, InstallLocation, InstallSource, InstallDate, Publisher, LocalPackage - Property
        <ProductName="VMware Infrastructure Update" VersionString="2.5.0.64237" InstallLocation="C:\Program Files\VMware\Infrastructure\" InstallSource="C:\Users\WHARGR~1\AppData\Local\Temp\_isA1FB\" InstallDate="20110721" Publisher="VMware, Inc." LocalPackage="C:\Windows\Installer\7124493c.msi"/>
        <ProductName="NT Testing TCP Tool" VersionString="1.0.0" InstallSource="C:\Users\whargrove\Downloads\" InstallDate="20130211" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\20788d8c.msi"/>
        <ProductName="inSSIDer" VersionString="2.1.3" InstallSource="C:\Users\whargrove\Downloads\" InstallDate="20120605" Publisher="MetaGeek" LocalPackage="C:\Windows\Installer\8eb9c024.msi"/>
        <ProductName="MessageOps Exporter" VersionString="1.20" InstallSource="C:\Users\whargrove\AppData\Local\Temp\Temp1_MOExport1.2.zip\" InstallDate="20130409" Publisher="MessageOps" LocalPackage="C:\Windows\Installer\55a29199.msi"/>
        <ProductName="Aventail Access Manager" VersionString="10.53.52" InstallSource="C:\Users\WHARGR~1\AppData\Local\Temp\" InstallDate="20111214" Publisher="SonicWALL Inc" LocalPackage="C:\Windows\Installer\5139fce3.msi"/>
        <ProductName="HumanConcepts OrgPlus 8 Plug-in" VersionString="8.3.1195.0" InstallLocation="C:\Program Files\Common Files\HumanConcepts\OrgPlus 8 Plug-in\" InstallSource="C:\Users\WHARGR~1\AppData\Local\Temp\{5AB92F13-75A0-4FCC-90FA-AC9F078CA828}\" InstallDate="20110506" Publisher="HumanConcepts" LocalPackage="C:\Windows\Installer\7f0e109.msi"/>
        <ProductName="Google Talk Plugin" VersionString="4.1.3.13728" InstallSource="C:\Users\whargrove\AppData\Local\Google\Update\Install\{BAF66C66-BE24-43FD-BA71-1652286B168A}\" InstallDate="20130627" Publisher="Google" LocalPackage="C:\Windows\Installer\4ec819ea.msi"/>
        <ProductName="Microsoft Office Visio MUI (English) 2007" VersionString="12.0.6612.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90120000-0054-0409-0000-0000000FF1CE}-C\" InstallDate="20111108" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\32a4511.msi"/>
        <ProductName="Microsoft Office Shared Setup Metadata MUI (English) 2007" VersionString="12.0.6612.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90120000-0115-0409-0000-0000000FF1CE}-C\" InstallDate="20111108" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\ad65c.msi"/>
        <ProductName="Microsoft Office Proofing (English) 2007" VersionString="12.0.4518.1014" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90120000-002C-0409-0000-0000000FF1CE}-C\" InstallDate="20100223" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\ad69a.msi"/>
        <ProductName="Microsoft Office Shared MUI (English) 2007" VersionString="12.0.6612.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90120000-0115-0409-0000-0000000FF1CE}-C\" InstallDate="20111108" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\ad656.msi"/>
        <ProductName="Microsoft Office Proof (English) 2007" VersionString="12.0.6612.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90120000-002C-0409-0000-0000000FF1CE}-C\Proof.en\" InstallDate="20111108" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\ad694.msi"/>
        <ProductName="Microsoft Office Proof (Spanish) 2007" VersionString="12.0.6612.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90120000-002C-0409-0000-0000000FF1CE}-C\Proof.es\" InstallDate="20111108" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\ad686.msi"/>
        <ProductName="Microsoft Office Proof (French) 2007" VersionString="12.0.6612.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90120000-002C-0409-0000-0000000FF1CE}-C\Proof.fr\" InstallDate="20111108" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\ad68d.msi"/>
        <ProductName="Microsoft Office Visio Professional 2007" VersionString="12.0.6612.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{91120000-0051-0000-0000-0000000FF1CE}-C\" InstallDate="20130701" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\32a454d.msi"/>
        <ProductName="Microsoft Application Error Reporting" VersionString="12.0.6012.5000" InstallSource="C:\Program Files\Common Files\Windows Live\.cache\ce1168531ccf8a006\" InstallDate="20120302" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\bdfec60a.msi"/>
        <ProductName="Microsoft Office Professional Plus 2010" VersionString="14.0.6029.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90140000-0011-0000-0000-0000000FF1CE}-C\" InstallDate="20130701" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\2174b34.msi"/>
        <ProductName="Microsoft Office OneNote MUI (English) 2010" VersionString="14.0.6029.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90140000-00A1-0409-0000-0000000FF1CE}-C\" InstallDate="20111108" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\2c0e4c29.msi"/>
        <ProductName="Microsoft Office InfoPath MUI (English) 2010" VersionString="14.0.6029.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90140000-0044-0409-0000-0000000FF1CE}-C\" InstallDate="20110726" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\2173ff1.msi"/>
        <ProductName="Microsoft Office Access MUI (English) 2010" VersionString="14.0.6029.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90140000-0117-0409-0000-0000000FF1CE}-C\Access.en-us\" InstallDate="20110726" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\2c0e4c33.msi"/>
        <ProductName="Microsoft Office Shared Setup Metadata MUI (English) 2010" VersionString="14.0.6029.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90140000-0115-0409-0000-0000000FF1CE}-C\" InstallDate="20110726" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\2c0e4bfc.msi"/>
        <ProductName="Microsoft Office Excel MUI (English) 2010" VersionString="14.0.6029.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90140000-0016-0409-0000-0000000FF1CE}-C\" InstallDate="20110726" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\2c0e4c09.msi"/>
        <ProductName="Microsoft Office Access Setup Metadata MUI (English) 2010" VersionString="14.0.6029.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90140000-0117-0409-0000-0000000FF1CE}-C\" InstallDate="20110726" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\2c0e4c38.msi"/>
        <ProductName="Microsoft Office PowerPoint MUI (English) 2010" VersionString="14.0.6029.1000" InstallLocation="C:\Program Files\Microsoft Office\" InstallSource="C:\MSOCache\All Users\{90140000-0018-0409-0000-0000000FF1CE}-C\" InstallDate="20110726" Publisher="Microsoft Corporation" LocalPackage="C:\Windows\Installer\2c0e4c03.msi"/>

    </SoftwareInstalled>
</Computer>

我的数据库表的列是

ID       Category    Property Value Parent

我桌子的T-SQL创建脚本:

Create table AssetProperty
(
Id  int IDENTITY(100001, 1) NOT NULL,
Category varchar(120),
Property varchar(100),
Value varchar (max),
Parent varchar(50)
)

我希望将上面的xml数据保存到此表中。

从xml文件操作系统需要进入“类别”列,名称需要进入“属性”列,名称值应进入“值”列,父进程应为“管理员”

<LocalGroups>   
            <Group Name="Administrators">.

我将如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

这样的事情:

select
    T.C.value('local-name(.)', 'nvarchar(max)') as Category,
    A.C.value('local-name(.)', 'nvarchar(max)') as Property,
    A.C.value('.', 'nvarchar(max)') as Value
from @xml.nodes('//*') as T(C)
    outer apply T.C.nodes('./@*') as A(C)

=> sql fiddle demo