串联XML没有类型转换为字符串(Concatenate XML without type casti

2019-07-30 16:00发布

我从各种表中我的SQL Server数据库中生成以下XML

<XMLData>
...
<Type>1</Type>
...
</XMLData>

<XMLData>
...
<Type>2</Type>
...
</XMLData>

<XMLData>
...
<Type>3</Type>
...
</XMLData>

最终输出我需要的是一个合并如下:

<AllMyData>
    <XMLData>
        ...
        <Type>1</Type>
        ...
    </XMLData>
    <XMLData>
        ...
        <Type>2</Type>
        ...
    </XMLData>
    <XMLData>
        ...
        <Type>3</Type>
        ...
    </XMLData>
<AllMyData>

注-所有的,我结合具有相同的标记名称的独立元素。

在此先感谢为寻找这件事。

Answer 1:

我从各种表中我的SQL Server数据库中生成以下XML

取决于你如何拥有它,但如果它是在一个XML变量,你可以这样做。

declare @XML1 xml
declare @XML2 xml
declare @XML3 xml

set @XML1 = '<XMLData><Type>1</Type></XMLData>'
set @XML2 = '<XMLData><Type>2</Type></XMLData>'
set @XML3 = '<XMLData><Type>3</Type></XMLData>'

select @XML1, @XML2, @XML3 
for xml path('AllMyData')


Answer 2:

我不能发表评论,但可以回答这样即使我觉得评论是比较合适的,我会在什么上面rainabba回答添加更多的控制扩展。 我的.Net代码需要知道列名返回,所以我不能依靠自动生成的名字,但需要上面另有规定的最顶端rainabba。

这样,XML可以有效地连接成一个行和结果列命名。 你可以使用同样的方法将结果分配到一个XML变量,并返回从PROC也。

SELECT (
 SELECT XmlData as [*]
 FROM
     (
     SELECT
         xmlResult AS [*]
     FROM
         @XmlRes
     WHERE
         xmlResult IS NOT NULL
     FOR XML PATH(''), TYPE
     ) as DATA(XmlData)
 FOR XML PATH('')
) as [someColumnName]


Answer 3:

如果您使用for xml type ,你可以结合XML列没有铸造他们。 例如:

select  *
from    (
        select  (
                select  1 as Type
                for xml path(''), type
                )
        union all
        select  (
                select  2 as Type
                for xml path(''), type
                )
        union all
        select  (
                select  3 as Type
                for xml path(''), type
                )
        ) as Data(XmlData)
for xml path(''), root('AllMyData'), type

这将打印:

<AllMyData>
    <XmlData>
        <Type>1</Type>
    </XmlData>
    <XmlData>
        <Type>2</Type>
    </XmlData>
    <XmlData>
        <Type>3</Type>
    </XmlData>
</AllMyData>


Answer 4:

作为附录的Mikael埃里克森的答案 - 如果你有,你需要不断地添加节点,然后想组一个过程,一个节点下,这是做这件事:

declare @XML1 XML
declare @XML2 XML
declare @XML3 XML
declare @XMLSummary XML

set @XML1 = '<XMLData><Type>1</Type></XMLData>'
set @XMLSummary = (SELECT @XMLSummary, @XML1 FOR XML PATH(''))

set @XML2 = '<XMLData><Type>2</Type></XMLData>'
set @XMLSummary = (SELECT @XMLSummary, @XML2 FOR XML PATH(''))

set @XML3 = '<XMLData><Type>3</Type></XMLData>'
set @XMLSummary = (SELECT @XMLSummary, @XML3 FOR XML PATH(''))


SELECT @XMLSummary FOR XML PATH('AllMyData')


Answer 5:

我需要做相同的,但不知道多少行/变量是如何关心和不加所以这里额外的架构是我的解决方案。 根据这一模式,我可以生成尽可能多的片段,因为我想,将它们结合起来,把它们PROCS之间,甚至从特效和在任何时候回报他们,在容器包装起来都没有修改数据或被迫加入XML结构成我的数据。 我用HTTP端点这种方式来提供XML Web服务和与XML转换成JSON,提供Web服务的JSON另一个伎俩。

    -- SETUP A type (or use this design for a Table Variable) to temporarily store snippets into. The pattern can be repeated to pass/store snippets to build
    -- larger elements and those can be further combined following the pattern.
    CREATE TYPE [dbo].[XMLRes] AS TABLE(
        [xmlResult] [xml] NULL
    )
    GO


    -- Call the following as much as you like to build up all the elements you want included in the larger element
    INSERT INTO @XMLRes ( xmlResult )
        SELECT
            (    
                SELECT
                    'foo' '@bar'
                FOR XML
                    PATH('SomeTopLevelElement')
            )

    -- This is the key to "concatenating" many snippets into a larger element. At the end of this, add " ,ROOT('DocumentRoot') " to wrapp them up in another element even
    -- The outer select is a time from user2503764 that controls the output column name

   SELECT (
    SELECT XmlData as [*]
    FROM
        (
        SELECT
            xmlResult AS [*]
        FROM
            @XmlRes
        WHERE
            xmlResult IS NOT NULL
        FOR XML PATH(''), TYPE
        ) as DATA(XmlData)
    FOR XML PATH('')
   ) as [someColumnName]


Answer 6:

ALTER PROCEDURE usp_fillHDDT @Code  int

AS
BEGIN

 DECLARE @HD XML,@DT XML;  

    SET NOCOUNT ON;
    select invhdcode, invInvoiceNO,invDate,invCusCode,InvAmount into #HD
    from dbo.trnInvoiceHD where invhdcode=@Code

    select invdtSlNo No,invdtitemcode ItemCode,invdtitemcode ItemName,
    invDtRate Rate,invDtQty Qty,invDtAmount Amount ,'Kg' Unit into #DT from
     dbo.trnInvoiceDt  where invDtTrncode=@Code 

    set @HD = (select * from #HD HD  FOR XML AUTO,ELEMENTS XSINIL);
    set @DT = (select* from #DT DT FOR XML AUTO,ELEMENTS XSINIL);

    SELECT CAST ('<OUTPUT>'+ CAST (ISNULL(@HD,'') AS VARCHAR(MAX))+ CAST ( ISNULL(@DT,'') AS VARCHAR(MAX))+ '</OUTPUT>'   AS XML)

END


Answer 7:

public String ReplaceSpecialChar(String inStr)
{
    inStr = inStr.Replace("&", "&amp;");
    inStr = inStr.Replace("<", "&lt;");
    inStr = inStr.Replace(">", "&gt;");
    inStr = inStr.Replace("'", "&#39;");
    inStr = inStr.Replace("\"", "&quot;");
    return inStr;
}


文章来源: Concatenate XML without type casting to string