我正在尋找一種將開始時添加此節點「LoanSecondaryStatusDates」並將其相應的結束標記「LoanSecondaryStatusDates」添加到結尾的方法。我在SQL服務器中使用「FOR XML」生成了下面的內容,但無法弄清楚如何添加開始和結束標記。如果這可以使用「FOR XML」,那麼一個例子會很好,但是C#會更好。謝謝!如何在節點集合之前和之後添加XML節點
目前:
<Loans>
<Loan>
<GUID></GUID>
<AgentCompanyName></AgentCompanyName>
<LoanSecondaryStatus>
<StatusName>Name</StatusName>
<StatusDate>Date</StatusDate>
</LoanSecondaryStatus>
<LoanSecondaryStatus>
<StatusName>Name</StatusName>
<StatusDate>Date</StatusDate>
</LoanSecondaryStatus>
</Loan>
</Loans>
最終結果應該是:
<Loans>
<Loan>
<GUID></GUID>
<AgentCompanyName></AgentCompanyName>
<LoanSecondaryStatusDates>
<LoanSecondaryStatus>
<StatusName>Name</StatusName>
<StatusDate>Date</StatusDate>
</LoanSecondaryStatus>
<LoanSecondaryStatus>
<StatusName>Name</StatusName>
<StatusDate>Date</StatusDate>
</LoanSecondaryStatus>
</LoanSecondaryStatusDates>
</Loan>
</Loans>
FOR XML查詢
SELECT
[GUID]
,[AgentCompanyName],
(
SELECT
'Borrower Docs Sent/Req' as 'StatusName',
CASE WHEN t.BorrowerDocsSent IS NOT NULL THEN t.BorrowerDocsSent ELSE '' END as 'StatusDate'
FROM Encompass_loanData as t
WHERE t.[GUID] = E.[GUID]
FOR XML PATH('LoanSecondaryStatus'), TYPE
),
(
SELECT
t.BorrowerCity as 'StatusName',
t.[GUID] as 'StatusDate'
FROM Encompass_loanData as t
WHERE t.[GUID] = E.[GUID]
FOR XML PATH('LoanSecondaryStatus'), TYPE
)
From Encompass_loanData E
FOR XML PATH ('Loan'), type, root('Loans')
SQL小提琴:http://sqlfiddle.com/#!6/d672a/2/0
包裹子元素謝謝你的XSL作品,但它正在剝離XML文檔中的其他元素。有沒有辦法保持現有的結構相同,但只是按照上面的格式設置這一部分? – dc922
我的歉意。我已經添加了FOR XML查詢。 – dc922