我需要使用SSIS包将一些数据从XML文件导入到SQL Server,但是当我生成xsd文件(来自SSIS)时,XML文件中的一些必要数据不会映射为要输出的列:
这是我正在使用的一个非常相似的xml文件模型:
需要导入第2行中的数据(DocumentID,ExportID,JobID,RunID,CreationTime,StartTime,EndTime)。
以下是从SSIS生成XSD文件的方法:
<?xml version="1.0"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:com:ssn:schema:export:SSNExportFormat.xsd">
<xsd:element name="SSNExportDocument">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="MeterData">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="RegisterData">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="RegisterRead">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="Tier">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" name="Register">
<xsd:complexType>
<xsd:attribute name="Number" type="xsd:unsignedShort" use="optional" />
<xsd:attribute name="Summation" type="xsd:decimal" use="optional" />
<xsd:attribute name="SummationUOM" type="xsd:string" use="optional" />
<xsd:attribute name="CumulativeDemand" type="xsd:decimal" use="optional" />
<xsd:attribute name="MaximumDemand" type="xsd:decimal" use="optional" />
<xsd:attribute name="MaximumDemandTime" type="xsd:dateTime" use="optional" />
<xsd:attribute name="DemandUOM" type="xsd:string" use="optional" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="Number" type="xsd:unsignedByte" use="optional" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="ReadTime" type="xsd:dateTime" use="optional" />
<xsd:attribute name="GatewayCollectedTime" type="xsd:dateTime" use="optional" />
<xsd:attribute name="RegisterReadSource" type="xsd:string" use="optional" />
<xsd:attribute name="Season" type="xsd:unsignedByte" use="optional" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="StartTime" type="xsd:dateTime" use="optional" />
<xsd:attribute name="EndTime" type="xsd:dateTime" use="optional" />
<xsd:attribute name="NumberReads" type="xsd:unsignedByte" use="optional" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="MeterName" type="xsd:unsignedInt" use="optional" />
<xsd:attribute name="UtilDeviceID" type="xsd:string" use="optional" />
<xsd:attribute name="MacID" type="xsd:string" use="optional" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="Version" type="xsd:decimal" use="optional" />
<xsd:attribute name="DocumentID" type="xsd:string" use="optional" />
<xsd:attribute name="ExportID" type="xsd:string" use="optional" />
<xsd:attribute name="JobID" type="xsd:unsignedByte" use="optional" />
<xsd:attribute name="RunID" type="xsd:unsignedByte" use="optional" />
<xsd:attribute name="CreationTime" type="xsd:dateTime" use="optional" />
<xsd:attribute name="StartTime" type="xsd:dateTime" use="optional" />
<xsd:attribute name="EndTime" type="xsd:dateTime" use="optional" />
</xsd:complexType>
</xsd:element>
</xs:schema>
我是SSIS和XML任务的新手,我将不胜感激任何帮助! TKS ...
答案 0 :(得分:1)
我认为您无法使用XML Source
组件实现此目的,您必须添加脚本组件(作为源),打开脚本组件编辑器,添加所需的输出列(DocumentID,ExportID) ,JobID,RunID,CreationTime,StartTime,EndTime)。
然后打开脚本编辑器并编写以下代码(我使用VB.NET):
您必须将"C:\MyPath\xmlfile.xml"
替换为xml文件路径,您还必须添加System.Xml.Linq
dll作为参考
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
Imports System.Xml
Imports System.Xml.Linq
<Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute>
<CLSCompliant(False)>
Public Class ScriptMain
Inherits UserComponent
Public Overrides Sub CreateNewOutputRows()
Dim strFilePath As String = "C:\MyPath\xmlfile.GetXmlNamespace"
Dim xml As XDocument = XDocument.Load(strFilePath)
Output0Buffer.AddRow()
With Output0Buffer
.DocumentID = If(xml.Root.Attribute("DocumentID") Is Nothing, "", xml.Root.Attribute("DocumentID").Value.ToString)
.ExportID = If(xml.Root.Attribute("ExportID") Is Nothing, "", xml.Root.Attribute("ExportID").Value.ToString)
.JobID = If(xml.Root.Attribute("JobID") Is Nothing, "", xml.Root.Attribute("JobID").Value.ToString)
.RunID = If(xml.Root.Attribute("RunID") Is Nothing, "", xml.Root.Attribute("RunID").Value.ToString)
.CreationTime = If(xml.Root.Attribute("CreationTime") Is Nothing, "", xml.Root.Attribute("CreationTime").Value.ToString)
.StartTime = If(xml.Root.Attribute("StartTime") Is Nothing, "", xml.Root.Attribute("StartTime").Value.ToString)
.EndTime = If(xml.Root.Attribute("EndTime") Is Nothing, "", xml.Root.Attribute("EndTime").Value.ToString)
End With
End Sub
End Class