从SQL查询数据结果创建个性化XML

时间:2018-09-05 20:33:29

标签: xml sql-server-2012

我正在尝试通过SQL数据结果以特定格式创建xml结果。但是我不知道我将如何产生。我知道一种基本的呼叫方式

当前按照下面的Sql Query

DECLARE @cityFrom VARCHAR(10)= 'Berlin';
DECLARE @CustomerFrom VARCHAR(25)= CASE
     WHEN @cityFrom = 'Berlin'
     THEN 'ID_1'
     WHEN @cityFrom = 'London'
     THEN 'ID_2'
     WHEN @cityFrom = 'Tsawassen'
     THEN 'ID_3'
     WHEN @cityFrom = 'Mannheim'
     THEN 'ID_4'
 END;

    SELECT CustomerID,
       CustomerName,
       Country
    FROM Customers
    WHERE City = @cityFrom;
FOR XML PATH (''), ROOT('table') 

我得到了这个输出

<table>
  <CustomerID>1</CustomerID>
  <CustomerName>Alfreds Futterkiste</CustomerName>
  <Country>Germany</Country>
</table>

但是我希望获得如下预期结果,

<ID_1>
    <Parameter key="ID_1_CustomerID">1</Parameter>
    <Parameter key="ID_1_CustomerName">Alfreds Futterkiste</Parameter>
    <Parameter key="ID_1_Country">Germany</Parameter>
</ID_1>

我正在致电@CustomerFrom,因为我想将其发布为<ID_1>...</ID_1>。我也知道<table>...</table>是通过ROOT('table')发布的,但是我无法传递ROOT(@CustomerFrom)作为...

问题:有什么解决办法可以得到预期的结果吗?

2 个答案:

答案 0 :(得分:2)

用变量命名元素是一个坏主意(稍后会对此进行更多介绍)。但这可以解决:

DECLARE @mockup TABLE(CustomerID INT, CustomerName VARCHAR(100),Country VARCHAR(100),City VARCHAR(100));
INSERT INTO @mockup VALUES(99,'TestName','Germany','Berlin');

DECLARE @cityFrom VARCHAR(10)= 'Berlin';

DECLARE @CustomerFrom VARCHAR(25)= CASE
     WHEN @cityFrom = 'Berlin'
     THEN 'ID_1'
     WHEN @cityFrom = 'London'
     THEN 'ID_2'
     WHEN @cityFrom = 'Tsawassen'
     THEN 'ID_3'
     WHEN @cityFrom = 'Mannheim'
     THEN 'ID_4'
 END;

SELECT @CustomerFrom + '_CustomerID' AS [Parameter/@key]
       ,CustomerID AS [Parameter]
       ,''
       ,@CustomerFrom + '_CustomerName' AS [Parameter/@key]
       ,CustomerName AS [Parameter]
       ,''
       ,@CustomerFrom + '_Country' AS [Parameter/@key]
       ,Country AS [Parameter]
FROM @mockup
FOR XML PATH('ReplaceThis');

下一步,您必须将其强制转换为NVARCHAR(MAX)并使用REPLACEID_1放入根节点...

我不知道预期结果是否需要这样...

将信息( content )推送到元素名称中是一个非常糟糕的主意。我建议不要使用<ID_1>之类的东西。

我将使用两个属性而不是<ID location="1">或从上一级获取此信息。

尝试一下:

ID_1_CustomerID

结果

SELECT @CustomerFrom AS [@location]
       ,'CustomerID' AS [Parameter/@key]
       ,CustomerID AS [Parameter]
       ,''
       ,'CustomerName' AS [Parameter/@key]
       ,CustomerName AS [Parameter]
       ,''
       ,'Country' AS [Parameter/@key]
       ,Country AS [Parameter]
FROM @mockup
FOR XML PATH('ID');

答案 1 :(得分:0)

使用Xml Linq尝试一下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = "<table>\n" +
                            "<CustomerID>1</CustomerID>\n" +
                            "<CustomerName>Alfreds Futterkiste</CustomerName>\n" +
                            "<Country>Germany</Country>\n" +
                            "</table>";

            XElement table = XElement.Parse(xml);
            int id = (int)table.Element("CustomerID");

            XElement xID = new XElement("ID_1");

            foreach (XElement element in table.Elements())
            {
                xID.Add(new XElement("Parameter",  new object[]  {new XAttribute("key", "ID_" + id.ToString() + "_" + element.Name.LocalName), (string)element}));
            }


        }
    }
}