Sql server 使用TSQL读取XML

Sql server 使用TSQL读取XML,sql-server,xml,tsql,Sql Server,Xml,Tsql,我有这个XML,我试图读取XML中的Color值。我正在使用这个T-SQL查询 ;WITH XMLNAMESPACES('http://schemas.datacontract.org/2004/07/CubeMasterWebService' As a, default 'http://tempuri.org/') SELECT t.c.value('a:Color[1]', 'varchar(100)') Color FROM @XmlData.nodes('/a:tS

我有这个XML,我试图读取XML中的
Color
值。我正在使用这个T-SQL查询

;WITH XMLNAMESPACES('http://schemas.datacontract.org/2004/07/CubeMasterWebService' As a, default 'http://tempuri.org/')
SELECT 
    t.c.value('a:Color[1]', 'varchar(100)') Color
FROM  
    @XmlData.nodes('/a:tShipment/a:CargoGroups/a:Cargoes/a:Cargo') AS t(c)
XmlData
是一个包含以下内容的XML变量:

<Run xmlns="http://tempuri.org/">
   <tShipment xmlns:a="http://schemas.datacontract.org/2004/07/CMWS" 
              xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <a:CargoGroups/>
      <a:Cargoes>
         <a:Cargo>
           <a:Alias i:nil="true"/>
           <a:Alias2 i:nil="true"/>
           <a:Color>0</a:Color>
           <a:Description i:nil="true"/>
           <a:FloorStackSupportsOthers>false</a:FloorStackSupportsOthers>
           <a:FloorStackType>BestFit</a:FloorStackType>
           <a:GroupName>Stop 1</a:GroupName>
           <a:Height>34.25</a:Height>
           <a:Length>34.17</a:Length>
           <a:MaxLayersAllowedOverDifferentCargoes>false</a:MaxLayersAllowedOverDifferentCargoes>
           <a:MaxLayersOnOrientation1>0</a:MaxLayersOnOrientation1>
           <a:MaxLayersOnOrientation2>0</a:MaxLayersOnOrientation2>
           <a:MaxLayersOnOrientation3>0</a:MaxLayersOnOrientation3>
           <a:MaxLayersOnOrientation4>0</a:MaxLayersOnOrientation4>
           <a:MaxLayersOnOrientation5>0</a:MaxLayersOnOrientation5>
           <a:MaxLayersOnOrientation6>0</a:MaxLayersOnOrientation6>
           <a:Name>EXF120D</a:Name>
           <a:OrderPieces>0</a:OrderPieces>
           <a:OrientationPriority i:nil="true"/>
           <a:OrientationsAllowed>OrientationsBasic</a:OrientationsAllowed>
           <a:OverhangAllowed>false</a:OverhangAllowed>
           <a:OverhangLength>0</a:OverhangLength>
           <a:OverhangWidth>0</a:OverhangWidth>
           <a:Palletizing i:nil="true"/>
           <a:PieceInside>0</a:PieceInside>
           <a:Price>0</a:Price>
           <a:Property1 i:nil="true"/>
           <a:Property10 i:nil="true"/>
           <a:Property2 i:nil="true"/>
           <a:Property3 i:nil="true"/>
           <a:Property4 i:nil="true"/>
           <a:Property5 i:nil="true"/>
           <a:Property6 i:nil="true"/>
           <a:Property7 i:nil="true"/>
           <a:Property8 i:nil="true"/>
           <a:Property9 i:nil="true"/>
           <a:Qty>60</a:Qty>
           <a:Sequence>0</a:Sequence>
           <a:SetRatio>0</a:SetRatio>
           <a:StackValue>0</a:StackValue>
           <a:TurnAllowedOnFloor>false</a:TurnAllowedOnFloor>
           <a:Unitload i:nil="true"/>
           <a:UnitloadBottomPattern>0</a:UnitloadBottomPattern>
           <a:UnitloadLayersQtyRotated>0</a:UnitloadLayersQtyRotated>
           <a:UnitloadTopPattern>0</a:UnitloadTopPattern>
           <a:Weight>0</a:Weight>
           <a:Width>39.84</a:Width>
        </a:Cargo>
        <a:Cargo>
                    <a:Alias i:nil="true"/>
                    <a:Alias2 i:nil="true"/>
                    <a:Color>0</a:Color>
                    <a:Description i:nil="true"/>
                    <a:FloorStackSupportsOthers>false</a:FloorStackSupportsOthers>
                    <a:FloorStackType>BestFit</a:FloorStackType>
                    <a:GroupName>Stop 1</a:GroupName>
                    <a:Height>34.25</a:Height>
                    <a:Length>14.17</a:Length>
                    <a:MaxLayersAllowedOverDifferentCargoes>false</a:MaxLayersAllowedOverDifferentCargoes>
                    <a:MaxLayersOnOrientation1>0</a:MaxLayersOnOrientation1>
                    <a:MaxLayersOnOrientation2>0</a:MaxLayersOnOrientation2>
                    <a:MaxLayersOnOrientation3>0</a:MaxLayersOnOrientation3>
                    <a:MaxLayersOnOrientation4>0</a:MaxLayersOnOrientation4>
                    <a:MaxLayersOnOrientation5>0</a:MaxLayersOnOrientation5>
                    <a:MaxLayersOnOrientation6>0</a:MaxLayersOnOrientation6>
                    <a:Name>EXF120E</a:Name>
                    <a:OrderPieces>0</a:OrderPieces>
                    <a:OrientationPriority i:nil="true"/>
                    <a:OrientationsAllowed>OrientationsDefault</a:OrientationsAllowed>
                    <a:OverhangAllowed>false</a:OverhangAllowed>
                    <a:OverhangLength>0</a:OverhangLength>
                    <a:OverhangWidth>0</a:OverhangWidth>
                    <a:Palletizing i:nil="true"/>
                    <a:PieceInside>0</a:PieceInside>
                    <a:Price>0</a:Price>
                    <a:Property1 i:nil="true"/>
                    <a:Property10 i:nil="true"/>
                    <a:Property2 i:nil="true"/>
                    <a:Property3 i:nil="true"/>
                    <a:Property4 i:nil="true"/>
                    <a:Property5 i:nil="true"/>
                    <a:Property6 i:nil="true"/>
                    <a:Property7 i:nil="true"/>
                    <a:Property8 i:nil="true"/>
                    <a:Property9 i:nil="true"/>
                    <a:Qty>30</a:Qty>
                    <a:Sequence>0</a:Sequence>
                    <a:SetRatio>0</a:SetRatio>
                    <a:StackValue>0</a:StackValue>
                    <a:TurnAllowedOnFloor>false</a:TurnAllowedOnFloor>
                    <a:Unitload i:nil="true"/>
                    <a:UnitloadBottomPattern>0</a:UnitloadBottomPattern>
                    <a:UnitloadLayersQtyRotated>0</a:UnitloadLayersQtyRotated>
                    <a:UnitloadTopPattern>0</a:UnitloadTopPattern>
                    <a:Weight>0</a:Weight>
                    <a:Width>39.84</a:Width>
                </a:Cargo>
        </a:Cargoes>
        <a:Containers>
                <a:Container>
                    <a:CartonType>Tuck</a:CartonType>
                    <a:ContainerType>SeaVan</a:ContainerType>
                    <a:EmptyWeight>0</a:EmptyWeight>
                    <a:Height>110.2362</a:Height>
                    <a:Length>625.9843</a:Length>
                    <a:MaxHeight>0</a:MaxHeight>
                    <a:MaxVolPercent>0</a:MaxVolPercent>
                    <a:MaxWeight>0</a:MaxWeight>
                    <a:Name>40FT Dry</a:Name>
                    <a:PalletThickness>0</a:PalletThickness>
                    <a:PalletType>Wood2WaysDoube</a:PalletType>
                    <a:Priority>10</a:Priority>
                    <a:Qty>0</a:Qty>
                    <a:VehicleType>Dry</a:VehicleType>
                    <a:Width>99.2126</a:Width>
        </a:Container>
    </a:Containers>
    <a:Description>in C# code</a:Description>
            <a:Rules>
                <a:AlgorithmType>Default</a:AlgorithmType>
                <a:CalculationType>MixLoad</a:CalculationType>
                <a:FillDirection>FrontToRear</a:FillDirection>
                <a:IsBestFitContainersSelection>true</a:IsBestFitContainersSelection>
                <a:IsGroupUsed>true</a:IsGroupUsed>
                <a:IsPalletized>false</a:IsPalletized>
                <a:IsSafeStackingUsed>true</a:IsSafeStackingUsed>
                <a:IsSequenceUsed>false</a:IsSequenceUsed>
                <a:IsSpreadIdenticalCargoAllowed>true</a:IsSpreadIdenticalCargoAllowed>
                <a:IsTuned>true</a:IsTuned>
                <a:IsUnitloadFirst>false</a:IsUnitloadFirst>
                <a:IsWeightLimited>true</a:IsWeightLimited>
                <a:MaxRunTimeSeconds>0</a:MaxRunTimeSeconds>
                <a:MaxSearchDepth>3</a:MaxSearchDepth>
                <a:MaxSearchVolPerncentage>80</a:MaxSearchVolPerncentage>
                <a:MinSupportRate>90</a:MinSupportRate>
                <a:MixPallet>
                    <a:CartonType>Tuck</a:CartonType>
                    <a:ContainerType>SeaVan</a:ContainerType>
                    <a:EmptyWeight>0</a:EmptyWeight>
                    <a:Height>0</a:Height>
                    <a:Length>0</a:Length>
                    <a:MaxHeight>0</a:MaxHeight>
                    <a:MaxVolPercent>0</a:MaxVolPercent>
                    <a:MaxWeight>0</a:MaxWeight>
                    <a:Name>New</a:Name>
                    <a:PalletThickness>0</a:PalletThickness>
                    <a:PalletType>Wood2WaysDoube</a:PalletType>
                    <a:Priority>0</a:Priority>
                    <a:Qty>0</a:Qty>
                    <a:VehicleType>Dry</a:VehicleType>
                    <a:Width>0</a:Width>
                </a:MixPallet>
                <a:StackingRule>BestFit</a:StackingRule>
            </a:Rules>
            <a:Title>demo with receive</a:Title>
   </tShipment>
</Run>

0
假的
最佳匹配
第一站
34.25
34.17
假的
0
0
0
0
0
0
EXF120D
0
方向基本
假的
0
0
0
0
60
0
0
0
假的
0
0
0
0
39.84
0
假的
最佳匹配
第一站
34.25
14.17
假的
0
0
0
0
0
0
EXF120E
0
方向默认值
假的
0
0
0
0
30
0
0
0
假的
0
0
0
0
39.84
卷起
水车
0
110.2362
625.9843
0
0
0
40英尺干燥
0
Wood2WaysDoube
10
0
干的
99.2126
在C#代码中
违约
混装
前方
真的
真的
假的
真的
假的
真的
真的
假的
真的
0
3.
80
90
卷起
水车
0
0
0
0
0
0
新的
0
Wood2WaysDoube
0
0
干的
0
最佳匹配
带接收的演示
试试这个:

;WITH XMLNAMESPACES('http://schemas.datacontract.org/2004/07/CMWS' As a, 
                    'http://tempuri.org/' as ns)
SELECT 
    Color = t.c.value('(a:Color)[1]', 'varchar(20)')
FROM  
    @XmlData.nodes('/ns:Run/ns:tShipment/a:Cargoes/a:Cargo') AS t(c)

有顶级的
元素在
里面,而
里面-但这都是而不是
里面,正如您指定的那样….

似乎我遗漏了其他东西。它也不起作用。但这是一个很好的机会。我不知道。我能直接给你一枚奖牌吗!?它起作用了perfectly@AbrahamForchue:您的
with XMLNAMESPACES
语句中带有
前缀的XML命名空间错误-它必须与XML文档中的完全相同!(您在XML文档中将
CubeMasterWebService
缩写为
CMWS
…)(