2016-05-04 55 views
0
declare @xmlWithElements xml = N' 
<Elements> 
    <Element Guid="bc7a96e1-1d8e-4339-ace0-41bf593036ee"> 
    <TradeDocument GUID="BC7A96E1-1D8E-4339-ACE0-41BF593036EE" > 
     <Numerator> 
     <Element Value="PAR" /> 
     <Element Value="2016" /> 
     </Numerator> 
     <Items> 
     <Item OrdinalNumber="1" ProductId="5045" ProductCode="qq" ProductNameVersion="1" Quantity="1.0000" QuantityInBasicUnit="1.0000" UnitId="1" CurrencyId="1" ToPay="167.1000" ToPayNet="135.8500" ProductInitialPrice="175.8900" ProductInitialNetPrice="143.0000" ProductEndPrice="167.1000" ProductEndNetPrice="136.1000" CalculatedDiscountPrice="-8.7900" CalculatedDiscountNetPrice="-6.7900" CalculatedDiscountInPercentage="5.0000" CalculatedDiscountValue="-8.7900" CalculatedDiscountNetValue="-6.7900" EmpoyeeDiscountValue="0.0000" EmpoyeeDiscountNetValue="0.0000" CumulativeDiscountInPercentage="0.0000" CumulativeDiscountValue="0.0000" CumulativeDiscountNetValue="0.0000" PriceCalculationPrecision="0" HeaderPercentageDiscount="0.0000" HeaderValueDiscount="0.0000" ManualUserDiscountInPercentage="0.0000" EmployeeDiscountInPercentage="0.0000" SelectedBarcodeId="0" LotId="4165" AssistantId="3" PriceTypeId="1" BundleCode="" BundleQuantity="0.0000" Points="0" QuantityBeforeCorrection="0.0000" QuantityAfterCorrection="0.0000" PurchaseValue="0.0000" SourceOrdinalNumber="0" VATRateId="2" /> 
     </Items> 
     <Payments> 
     <Payment NumberString="KP/2016/PKO BP/00013/" OrdinalNumber="1" PaymentFormId="4" CashBankAccountId="2" Type="1" SystemValue="167.10" CurrencyValue="167.10" CurrencyId="1" Numerator="1" Denominator="1" DocumentDate="2016-05-04 12:17:02.613" ExchangeRateDate="2016-04-29 10:05:43.297" AffectsCashRegisterBalance="true"/> 
     </Payments> 
     <VATAggregates> 
     <VATAggregate VATRateId="2" NetValue="135.85" GrossValue="167.10" VATValue="31.25" /> 
     </VATAggregates> 
    </TradeDocument> 
    </Element> 
</Elements>' 

SELECT 
    Guid = XElement.XProperty.value('@Guid','uniqueidentifier'), 
    Data = XElement.XProperty.value('(value)[1]','nvarchar(max)') 
FROM 
    @xmlWithElements.nodes('Elements/Element') AS XElement(XProperty) 

declare @xml2 nvarchar(max) = Convert(nvarchar(max), @xmlWithElements.query('/Elements/Element/*')) 

select @xml2 

我有特定的XML,我想讓每個孩子(元素名稱爲element)。Xml.nodes tsql - 空值

不幸的是,這種方法不起作用,我找不到任何工作解決方案(數據返回null)。

這段代碼有什麼問題?

UPDATE

當我在Data屬性使用

Data = XElement.XProperty.value('.','nvarchar(max)') 

我得到一個空列(NOT NULL)

+0

ou ter''在XML中沒有子節點'',這就是爲什麼'(value)[1]'返回null。現在,你想實際選擇什麼? – har07

+0

我想要得到一切什麼是在(這個例子我應該得到 ... ,但也可能位於不同的數據 – adamo94

回答

2

如果你 - 我希望正確理解 - 想讓內部XML只是作爲,你可以試試這個:

declare @xmlWithElements xml = 
N'<Elements> 
    <Element Guid="bc7a96e1-1d8e-4339-ace0-41bf593036ee"> 
    <TradeDocument GUID="BC7A96E1-1D8E-4339-ACE0-41BF593036EE" > 
     <Numerator> 
     <Element Value="PAR" /> 
     <Element Value="2016" /> 
     </Numerator> 
     <Items> 
     <Item OrdinalNumber="1" ProductId="5045" ProductCode="qq" ProductNameVersion="1" Quantity="1.0000" QuantityInBasicUnit="1.0000" UnitId="1" CurrencyId="1" ToPay="167.1000" ToPayNet="135.8500" ProductInitialPrice="175.8900" ProductInitialNetPrice="143.0000" ProductEndPrice="167.1000" ProductEndNetPrice="136.1000" CalculatedDiscountPrice="-8.7900" CalculatedDiscountNetPrice="-6.7900" CalculatedDiscountInPercentage="5.0000" CalculatedDiscountValue="-8.7900" CalculatedDiscountNetValue="-6.7900" EmpoyeeDiscountValue="0.0000" EmpoyeeDiscountNetValue="0.0000" CumulativeDiscountInPercentage="0.0000" CumulativeDiscountValue="0.0000" CumulativeDiscountNetValue="0.0000" PriceCalculationPrecision="0" HeaderPercentageDiscount="0.0000" HeaderValueDiscount="0.0000" ManualUserDiscountInPercentage="0.0000" EmployeeDiscountInPercentage="0.0000" SelectedBarcodeId="0" LotId="4165" AssistantId="3" PriceTypeId="1" BundleCode="" BundleQuantity="0.0000" Points="0" QuantityBeforeCorrection="0.0000" QuantityAfterCorrection="0.0000" PurchaseValue="0.0000" SourceOrdinalNumber="0" VATRateId="2" /> 
     </Items> 
     <Payments> 
     <Payment NumberString="KP/2016/PKO BP/00013/" OrdinalNumber="1" PaymentFormId="4" CashBankAccountId="2" Type="1" SystemValue="167.10" CurrencyValue="167.10" CurrencyId="1" Numerator="1" Denominator="1" DocumentDate="2016-05-04 12:17:02.613" ExchangeRateDate="2016-04-29 10:05:43.297" AffectsCashRegisterBalance="true"/> 
     </Payments> 
     <VATAggregates> 
     <VATAggregate VATRateId="2" NetValue="135.85" GrossValue="167.10" VATValue="31.25" /> 
     </VATAggregates> 
    </TradeDocument> 
    </Element> 
</Elements>'; 

SELECT Elmt.value('@Guid','uniqueidentifier') AS [Guid] 
     ,Elmt.query('*') AS [Data] 
FROM @xmlWithElements.nodes('Elements/Element') AS A(Elmt) 
+0

幾秒鐘:)更快我的方法[演示]( http://rextester.com/UXK23884) – lad2025