我正在寻找一种方法,在开头添加此节点“LoanSecondaryStatusDates”,并将相应的结束标记“LoanSecondaryStatusDates”添加到结尾。我在SQL服务器中使用“FOR XML”生成了以下内容,但无法弄清楚如何添加开始和结束标记。如果使用“FOR XML”可以做到这一点,那么一个例子会很棒,不过C#就没问题了。谢谢!
目前:
<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')
答案 0 :(得分:1)
您发布的xml没有有效的root。所以我添加了<xml>
作为根。
这也可以在C#中轻松完成。但这是一种使用xpath的方法。
<xml>
<LoanSecondaryStatus>
<StatusName>Name</StatusName>
<StatusDate>Date</StatusDate>
</LoanSecondaryStatus>
<LoanSecondaryStatus>
<StatusName>Name</StatusName>
<StatusDate>Date</StatusDate>
</LoanSecondaryStatus>
</xml>
这是XSL。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<LoanSecondaryStatusDates>
<xsl:for-each select="xml/LoanSecondaryStatus">
<LoanSecondaryStatus>
<StatusName>
<xsl:value-of select="StatusName"/>
</StatusName>
<StatusDate>
<xsl:value-of select="StatusDate"/>
</StatusDate>
</LoanSecondaryStatus>
</xsl:for-each>
</LoanSecondaryStatusDates>
</xsl:template>
</xsl:stylesheet>
输出:
<?xml version="1.0" encoding="utf-8"?>
<LoanSecondaryStatusDates>
<LoanSecondaryStatus>
<StatusName>Name</StatusName>
<StatusDate>Date</StatusDate>
</LoanSecondaryStatus>
<LoanSecondaryStatus>
<StatusName>Name</StatusName>
<StatusDate>Date</StatusDate>
</LoanSecondaryStatus>
</LoanSecondaryStatusDates>
更新了SQL查询
SELECT
[GUID]
,[AgentCompanyName],
(
SELECT NULL,
(
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 NULL AS X
FOR XML PATH('LoanSecondaryStatusDates'), 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
),
NULL
FOR XML PATH('LoanSecondaryStatusDates'),TYPE
)
FROM Encompass_loanData E
WHERE [LASTMODIFIED] >= '20160121'
FOR XML PATH ('Loan'), type, root('Loans')
答案 1 :(得分:0)
看看XElement类,你可以轻松地修改xml树。
https://msdn.microsoft.com/en-us/library/system.xml.linq.xelement(v=vs.110).aspx
答案 2 :(得分:0)
我能够用这个XSL包装子元素
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Loan">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::LoanSecondaryStatus)]"/>
<LoanSecondaryStatusDates>
<xsl:apply-templates select="LoanSecondaryStatus"/>
</LoanSecondaryStatusDates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>