【问题标题】:Getting data from sql and xml field Powershell从 sql 和 xml 字段 Powershell 获取数据
【发布时间】:2021-12-16 03:23:02
【问题描述】:

运行 sql 查询会生成如下表,其中 NTLdata 列是 xml 文件。

ID     Status     NTLdata        PLPData    

2365   Active    <com.123.ser..  <com.abc...
4962   Active    <com.123.req..  <com.abc...
899    Active    <com.123.ser..  <com.abc...

我正在像这样从 xml 文件中验证节点值-

$SqlConn= New-Object System.Data.SqlClient.SqlConnection
$SqlConn.ConnectionString= "Server= $server; Database=$DB"
$SqlConn.open()
$SqlCmd= New-Object New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText =$query
$SqlCmd.Connection=$SqlConn
$SqlAdapter= New-Object New-Object System.Data.SqlClient..SqlDataAdapter
$SqlAdapter.SelectCommand=$SqlCmd
$Dataset= New-Object.System.Data.DataSet
$SqlAdapte.Fill($DataSet) | Out-Null
$table= $DataSet.Tables[0]
$SqlConn.Close()
# retreieving xml column
$xmlCol= $table | %{ [xml]$_.NTLdata } #this has all the rows of NTLData as xml files type
$data= $xmlCol.SelectNodes("//Edition" | ? {$_.InnerText -eq '22'} 
# data has the <Edition> where value is 22
if ($data -ne $null) {
# *print the id from the column ID*
}

这里我想获取值为22的NTLdata列的所有行的xml文件。对于那些记录,我需要从sql查询返回表中获取对应的id。如何做到这一点?

示例 xml 文件

<com.123.req.abc.xyz>
<CheckPoint>
  <Number>025364</Number>
  <Cost>3333</Cost>
</CheckPoint>
<item>
  <code>18##</code>
  <Edition>22</Edition>
</item>
</com.123.req.abc.xyz>

假设此文件来自示例表的第二行,那么我希望显示 id 4962。我被困住了,没有想法!

【问题讨论】:

    标签: xml powershell


    【解决方案1】:

    尝试以下操作,它会输出那些表行的 ID,其中 .NTLdata 字段包含带有内部文本为 '22'Edition 元素的 XML:

    $table |
      Where-Object { ([xml] $_.NTLdata).SelectSingleNode('//Edition[.=22]') } |
        ForEach-Object Id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      相关资源
      最近更新 更多