【问题标题】:Generate XML File with custom tag from Object Variable in SSIS从 SSIS 中的对象变量生成带有自定义标记的 XML 文件
【发布时间】:2020-11-24 14:53:55
【问题描述】:

我正在尝试使用 SSIS 从 sql 表数据生成带有自定义标签和属性的 XML 文件。 我编写了从对象变量写入数据的 C# 脚本,但它给了我一个错误“对象不是 ADODB.RecordSet 或 ADODB.Record。参数名称:adodb”。 请帮忙。

提前致谢

【问题讨论】:

  • Object 变量不是 ADODB 记录集,而是 OLEDB 记录集。这是article with a code sample
  • 您好 Ferdipux,感谢您的回复我使用下面的代码将我的表格数据转换为 XML 文件,同样适用于其中一张表格。但现在它抛出了下面提到的错误“System.ArgumentException:'对象不是 ADODB.RecordSet 或 ADODB.Record。”数据表 dt; dt = (new DataSet("VariableName")).Tables.Add("Res"); (new OleDbDataAdapter()).Fill(dt, Dts.Variables["User::VariableName"] .Value.ToString()) 请您帮忙解决一下。提前致谢

标签: xml ssis


【解决方案1】:

根据原始问题更正了将 Object 变量表转换为 ADO.NET 表的 C# 代码。

DataTable dt = new DataTable();
(new OleDbDataAdapter()).Fill(dt, Dts.Variables["User::VariableName"].Value);

提示 - 您应该提供对象本身,而不是像在代码片段中那样将其转换为字符串。这就是像

这样的错误的原因

对象不是 ADODB.RecordSet 或 ADODB.Record。

【讨论】:

  • 非常感谢 Ferdipux 的帮助
【解决方案2】:

您可以不使用 SSIS 直接从 MSSQL 输出 xml,您可以使用列别名来设置您的自定义标签/属性。

请看下面的演示,属性用@前缀和标签没有任何前缀,子标签用/构建别名

使用子查询构建多行子标签

IF OBJECT_ID('tempdb.dbo.#StackOverflowTestParent') IS NOT NULL DROP TABLE #StackOverflowTestParent
CREATE TABLE #StackOverflowTestParent
(
    ParentId BIGINT PRIMARY KEY IDENTITY(1,1),
    ParentAttribute1 VARCHAR(50),
    ParentAttribute2 VARCHAR(50),
    ParentValue VARCHAR(50)
)

INSERT INTO #StackOverflowTestParent VALUES ('Oh','Hello','World')
INSERT INTO #StackOverflowTestParent VALUES ('World','Goes','Hello')

IF OBJECT_ID('tempdb.dbo.#StackOverflowTestParent') IS NOT NULL DROP TABLE #StackOverflowTestParent
CREATE TABLE #StackOverflowTestChild
(
    ChildId BIGINT PRIMARY KEY IDENTITY(1,1),
    ParentId BIGINT,
    ChildAttribute1 VARCHAR(50),
    ChildAttribute2 VARCHAR(50),
    ChildValue VARCHAR(50)
)

INSERT INTO #StackOverflowTestChild VALUES (1,'Bobby','Says','Hello')
INSERT INTO #StackOverflowTestChild VALUES (1,'Dobby','Starts','Waving')
INSERT INTO #StackOverflowTestChild VALUES (2,'Dolly','Says','Hozit')
INSERT INTO #StackOverflowTestChild VALUES (2,'Sarah','Says','Hi')

    IF OBJECT_ID('tempdb.dbo.#StackOverflowTestParent') IS NOT NULL DROP TABLE #StackOverflowTestParent
CREATE TABLE #StackOverflowTestParent
(
    ParentId BIGINT PRIMARY KEY IDENTITY(1,1),
    ParentAttribute1 VARCHAR(50),
    ParentAttribute2 VARCHAR(50),
    ParentValue VARCHAR(50)
)

INSERT INTO #StackOverflowTestParent VALUES ('Oh','Hello','World')
INSERT INTO #StackOverflowTestParent VALUES ('World','Goes','Hello')

IF OBJECT_ID('tempdb.dbo.#StackOverflowTestParent') IS NOT NULL DROP TABLE #StackOverflowTestParent
CREATE TABLE #StackOverflowTestChild
(
    ChildId BIGINT PRIMARY KEY IDENTITY(1,1),
    ParentId BIGINT,
    ChildAttribute1 VARCHAR(50),
    ChildAttribute2 VARCHAR(50),
    ChildValue VARCHAR(50)
)

INSERT INTO #StackOverflowTestChild VALUES (1,'Bobby','Says','Hello')
INSERT INTO #StackOverflowTestChild VALUES (1,'Dobby','Starts','Waving')
INSERT INTO #StackOverflowTestChild VALUES (2,'Dolly','Says','Hozit')
INSERT INTO #StackOverflowTestChild VALUES (2,'Sarah','Says','Hi')

;WITH XMLNAMESPACES ( 'www.example.com' AS ns0)
SELECT P.ParentId AS '@ParentId',
       P.ParentAttribute1 AS '@CustomAttribute1',
       P.ParentAttribute2 AS 'ParentSub/@CustomAttribute2',
       P.ParentValue AS 'StackOverflowTestMessage',
       /* Building child node with a subquery*/
       (SELECT C.ChildId AS '@ChildUniqueId',
               C.ChildAttribute1 AS '@ChildAttribute1',
               C.ChildAttribute2 AS 'SubChildSingle/@ChildAttribute2',
               C.ChildValue AS 'StackOverflowTestChild'
         FROM #StackOverflowTestChild C
         WHERE C.ParentId = P.ParentId
         FOR XML PATH('CustomChildNode'),TYPE)

FROM #StackOverflowTestParent P
FOR XML PATH('StackOverflowTestParent'),ROOT('ns0:Root')

结果

<ns0:Root xmlns:ns0="www.example.com">
  <StackOverflowTestParent ParentId="1"
                           CustomAttribute1="Oh">
    <ParentSub CustomAttribute2="Hello" />
    <StackOverflowTestMessage>World</StackOverflowTestMessage>
    <CustomChildNode xmlns:ns0="www.example.com"
                     ChildUniqueId="1"
                     ChildAttribute1="Bobby">
      <SubChildSingle ChildAttribute2="Says" />
      <StackOverflowTestChild>Hello</StackOverflowTestChild>
    </CustomChildNode>
    <CustomChildNode xmlns:ns0="www.example.com"
                     ChildUniqueId="2"
                     ChildAttribute1="Dobby">
      <SubChildSingle ChildAttribute2="Starts" />
      <StackOverflowTestChild>Waving</StackOverflowTestChild>
    </CustomChildNode>
  </StackOverflowTestParent>
  <StackOverflowTestParent ParentId="2"
                           CustomAttribute1="World">
    <ParentSub CustomAttribute2="Goes" />
    <StackOverflowTestMessage>Hello</StackOverflowTestMessage>
    <CustomChildNode xmlns:ns0="www.example.com"
                     ChildUniqueId="3"
                     ChildAttribute1="Dolly">
      <SubChildSingle ChildAttribute2="Says" />
      <StackOverflowTestChild>Hozit</StackOverflowTestChild>
    </CustomChildNode>
    <CustomChildNode xmlns:ns0="www.example.com"
                     ChildUniqueId="4"
                     ChildAttribute1="Sarah">
      <SubChildSingle ChildAttribute2="Says" />
      <StackOverflowTestChild>Hi</StackOverflowTestChild>
    </CustomChildNode>
  </StackOverflowTestParent>
</ns0:Root>

编辑

这是另一个如何创建包含两个命名空间的基本版本的示例

/* Here is a more basic version */
;WITH XMLNAMESPACES ( 'www.example.com' AS ns0, 'w3/XMLSchema-instance' AS xsi)
SELECT P.ParentAttribute1 AS 'ColumnNames/ParentAttribute1',
       P.ParentAttribute2 AS 'ColumnNames/ParentAttribute2',
       P.ParentValue AS 'ColumnNames/ParentValue'
FROM #StackOverflowTestParent P
FOR XML PATH('CustomTag'),ROOT('ns0:Root')

结果

<ns0:Root xmlns:xsi="w3/XMLSchema-instance" xmlns:ns0="www.example.com">
  <CustomTag>
    <ColumnNames>
      <ParentAttribute1>Oh</ParentAttribute1>
      <ParentAttribute2>Hello</ParentAttribute2>
      <ParentValue>World</ParentValue>
    </ColumnNames>
  </CustomTag>
  <CustomTag>
    <ColumnNames>
      <ParentAttribute1>World</ParentAttribute1>
      <ParentAttribute2>Goes</ParentAttribute2>
      <ParentValue>Hello</ParentValue>
    </ColumnNames>
  </CustomTag>
</ns0:Root>

【讨论】:

  • @csharpdev ,您使用 with 语句添加命名空间,已更新代码以进行演示。对于版本和字符编码,我通常只在 C# 中添加它,String xml = "" + sqlXMLOutput
  • 嵌入属性是什么意思?您可以使用列别名向任何节点添加属性或值?
  • 嗨 Ockert,非常感谢您的回复和 sql 脚本,这很有帮助实际上我需要以某种格式生成 XML 文件中的表数据,例如 w3/XMLSchema-instance"> 请告诉我如何使用 SQL 脚本添加相同的内容。提前致谢!
  • @csharpdev,在另一个例子中添加了,你需要更新路径名和别名来构建你想要的东西
  • 非常感谢奥克特的帮助
猜你喜欢
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多