我正在尝试将SQL Server 2012查询结果转换为格式化的XML结果。 我通常使用Visual Basic循环遍历记录集并形成它。
查询是:
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 & 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>
谢谢!
答案 0 :(得分:0)
不确定你在寻找什么,但我会像这样使用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