从SQL查询结果输出多级XML

从SQL查询结果输出多级XML,xml,sql-server-2012,Xml,Sql Server 2012,我正在尝试将SQL Server 2012查询结果转换为格式化的XML结果。 我通常使用VisualBasic循环记录集并形成它来完成这项工作 查询是: SELECT top 3 Plant_Devices.DeviceNumber, Plant_Devices.DeviceName, SYS_Templates.TemplateTag, Plant_Alarms_Groups.GroupTag FROM Plant_Devices INNER JOIN S

我正在尝试将SQL Server 2012查询结果转换为格式化的XML结果。 我通常使用VisualBasic循环记录集并形成它来完成这项工作

查询是:

SELECT top 3
    Plant_Devices.DeviceNumber, Plant_Devices.DeviceName, 
    SYS_Templates.TemplateTag, Plant_Alarms_Groups.GroupTag
FROM
    Plant_Devices 
INNER JOIN 
    SYS_Templates ON Plant_Devices.SYS_Templates_CID = SYS_Templates.CID 
INNER JOIN 
    Plant_Alarms_Groups ON Plant_Devices.Plant_Alarms_Groups_CID = Plant_Alarms_Groups.CID 
                        AND Plant_Devices.Plant_Alarms_Groups_CID = Plant_Alarms_Groups.CID
结果是:

DeviceNumber    DeviceName  TemplateTag GroupTag
1   Stucco & Dry Add. D/C Fan   D_Motor_Standard    WM1_StuccoSys
10  Kiln Drive  D_Drive_Standard    WM1_KilnSys
118 HRA Feeder Agitator #1  D_Motor_Standard    WM1_DryAdditives
所需的XML输出为:

<Tags>
   <Tag name="1" path="" type="UDT_INST">
      <Property name="Documentation" type="String">Stucco &amp; Dry Add. D/C Fan</Property>
      <Property name="DataType">2</Property>
      <Property name="UDTParentType">D_Motor_Standard</Property>
      <Parameters>
         <Property name="AlarmGroup" type="String">WM1_StuccoSys</Property>
         <Property name="DisplayName" type="String">1</Property>
         <Property name="ID" type="String">1</Property>
          </Parameters>
       </Tag>
       <Tag name="10" path="" type="UDT_INST">
          <Property name="Documentation" type="String">Kiln Drive</Property>
          <Property name="DataType">2</Property>
          <Property name="UDTParentType">D_Drive_Standard</Property>
          <Parameters>
             <Property name="AlarmGroup" type="String">WM1_KilnSys</Property>
             <Property name="DisplayName" type="String">10</Property>
             <Property name="ID" type="String">10</Property>
          </Parameters>
       <Tag name="118" path="" type="UDT_INST">
          <Property name="Documentation" type="String">HRA Feeder Agitator     #1</Property>
          <Property name="DataType">2</Property>
          <Property name="UDTParentType">D_Motor_Standard</Property>
          <Parameters>
             <Property name="AlarmGroup" type="String">WM1_DryAdditives</Property>
             <Property name="DisplayName" type="String">118</Property>
             <Property name="ID" type="String">118</Property>
          </Parameters>
       </Tag>
    </Tag>
</Tags>

灰泥及;干加。D/C风扇
2.
D_电机标准
WM1_灰泥系统
1.
1.
窑驱动
2.
D_驱动_标准
WM1_KilnSys
10
10
HRA给料机搅拌器#1
2.
D_电机标准
WM1_干燥添加剂
118
118

谢谢

不确定您在寻找什么,但我会像这样使用XML Linq

Imports System.Data
Imports System.Xml
Imports System.Xml.Linq
Module Module1

    Sub Main()
        Dim dt As New DataTable
        dt.Columns.Add("DeviceNumber", GetType(Integer))
        dt.Columns.Add("DeviceName", GetType(String))
        dt.Columns.Add("TemplateTag", GetType(String))
        dt.Columns.Add("GroupTag", GetType(String))

        dt.Rows.Add(New Object() {1, "Stucco & Dry Add. D/C Fan", "D_Motor_Standard", "WM1_StuccoSys"})
        dt.Rows.Add(New Object() {10, "Kiln Drive", "D_Drive_Standard", "WM1_KilnSys"})
        dt.Rows.Add(New Object() {118, "HRA Feeder Agitator #1", "D_Motor_Standard", "WM1_DryAdditives"})

        Dim tags As New XElement("Tags")

        For Each row As DataRow In dt.AsEnumerable
            Dim deviceNumber As Integer = row("DeviceNumber")
            Dim deviceName As String = row("DeviceName")
            Dim templateTag As String = row("TemplateTag")
            Dim groupTag As String = row("GroupTag")

            Dim tag As XElement = New XElement("Tag", New Object() {New XAttribute("name", deviceNumber), New XAttribute("path", ""), New XAttribute("type", "UDT_INST")})
            tags.Add(tag)


            Dim properties As List(Of XElement) = New List(Of XElement)({ _
                New XElement("Property", New Object() {New XAttribute("name", "Documentation"), New XAttribute("type", "String"), deviceName}), _
                New XElement("Property", New Object() {New XAttribute("name", "DataType"), 2}), _
                New XElement("Property", New Object() {New XAttribute("name", "UDTParentType"), templateTag}) _
            })
            tag.Add(properties)

            properties = New List(Of XElement)({ _
                New XElement("Property", New Object() {New XAttribute("name", "AlarmGroup"), New XAttribute("type", "String"), groupTag}), _
                New XElement("Property", New Object() {New XAttribute("name", "DisplayName"), New XAttribute("type", "String"), deviceNumber}), _
                New XElement("Property", New Object() {New XAttribute("name", "ID"), New XAttribute("type", "String"), deviceNumber}) _
            })

            Dim parameters As XElement = New XElement("Parameters", properties)
            tag.Add(parameters)
        Next
    End Sub

End Module
​

欢迎来到堆栈溢出。你可能会发现,你需要更具体地说明你到底想要达到什么样的最终结果。你想再次使用VB脚本吗?还是你在寻找另一个解决方案?我真的看不出这里有什么问题,它看起来更像是一个为我做这件事的人。我一直在尝试使用SQL server内部的xml生成器来输出所需的xml。但我似乎无法输出第二级和第三级分支。谢谢,我熟悉在Visual Basic内部执行此操作,我尝试使用查询和FOR XML选项在SQL Server内部生成此操作。您可以从Visual Basic中查询SQL Server并获取数据表。不确定是否可以在SQL Server中实现自定义xml。