【发布时间】:2020-06-24 20:07:51
【问题描述】:
示例 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<Searchable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" label="$RESX('Bank.1_8','CRDM_AgeInformation','Age Information')" xmlns="http://sysrepublic.com/Secure/4.0/DSL/SearchService">
<Parameters />
<Fields>
<Field data-type="System.Int64" is-editable="true" id="ageinformation_transactionid" label="$RESX('Bank.1_8','TransactionID','Transaction ID')" is-mandatory-display-field="false" is-hidden-display-field="false" is-virtual-date-time="false" is-display-field-only="false" use-utc-datetime="true" apply-user-timezone-offset="false" show-date="true" show-time="true" is-favourite="false" common-field="true">
<FieldDescription primary-key="false" nullable="false" readonly="false" hidden="false" format="text" enable-required-validation="true" />
<Operators>
<Operator name="equal" />
<Operator name="notequal" />
<Operator name="greaterthan" />
<Operator name="greaterthanequal" />
<Operator name="lessthan" />
<Operator name="lessthanequal" />
<Operator name="between" />
<Operator name="notbetween" />
<Operator name="in" />
<Operator name="notin" />
</Operators>
<LeftExpression>
<Field data-type="System.Int64" common-field="true">
<CollectionDescription collection="pos.CRDM_AgeInformation">
<Relationship parent="pos.CRDM_Header">
<RelatedField from="TransactionID" to="TransactionID" common-to-field="true" />
<RelatedField from="TradingDay" to="TradingDay" common-to-field="true" />
</Relationship>
</CollectionDescription>
<Description>TransactionID</Description>
</Field>
</LeftExpression>
</Field>
<Field data-type="System.Int64" is-editable="true" id="ageinformation_checkpointid" label="$RESX('Bank.1_8','CheckPointID','CheckPoint ID')" is-mandatory-display-field="false" is-hidden-display-field="false" is-virtual-date-time="false" is-display-field-only="false" use-utc-datetime="true" apply-user-timezone-offset="false" show-date="true" show-time="true" is-favourite="false">
<FieldDescription primary-key="false" nullable="false" readonly="false" hidden="false" format="text" enable-required-validation="true" />
<Operators>
<Operator name="equal" />
<Operator name="notequal" />
<Operator name="greaterthan" />
<Operator name="greaterthanequal" />
<Operator name="lessthan" />
<Operator name="lessthanequal" />
<Operator name="between" />
<Operator name="notbetween" />
<Operator name="in" />
<Operator name="notin" />
</Operators>
<LeftExpression>
<Field data-type="System.Int64">
<CollectionDescription collection="pos.CRDM_AgeInformation">
<Relationship parent="pos.CRDM_Header">
<RelatedField from="TransactionID" to="TransactionID" common-to-field="true" />
<RelatedField from="TradingDay" to="TradingDay" common-to-field="true" />
</Relationship>
</CollectionDescription>
<Description>CheckPointID</Description>
</Field>
</LeftExpression>
</Field>
</Fields>
</Searchable>
要解决的问题:
- 检查用于不同字段的 XML 标签是否存在于数据库表中。
- 如果确实存在,则什么也不做。
- 如果不存在,则更改节点标签。 () 中的中间值应替换为节点 ID。之后,如果被替换的值有下划线,用点替换
- 将更新的值发送到数据库中的测试表设置
下面是我试过的代码:
$SQLServer = "WIN-17V7QT0IJVK"
$SQLDBName = "Test"
$uid ="WIN-17V7QT0IJVK\Administrator"
$pwd = "letmebackinplease"
$ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString
$SqlConnection.open()
$xml = New-Object XML
$n = "C:\Users\Administrator\Desktop\test2.xml"
$xml.Load($n)
$nodes = $xml.Searchable.Fields.Field
# $regex = '()\(.*?\)'
$regex = '\((.*)\)'
foreach($node in $nodes) {
$a = $node.label
$a -match $regex
$data = $Matches[1]
$z = $data.split(',')
Write-Output($z[0])
Write-Output($z[1])
Write-Output($z[2])
$query = "
SELECT
count(ID) as cnt
FROM [Secure4].[secure].[Resource]
WHERE
[ResourceType] = " + $z[0]
$query += " AND [ResourceKey] = " + $z[1]
$query += " AND [ResourceValue] = " + $z[2]
Write-Output($query)
$Resource = (Invoke-SQLCmd -query $query -Server $SQLServer)
Write-Output($Resource)
$id = $node.id
Write-Output($id)
#$description = $node.Description
#Write-Output($description)
if($Resource -eq '0'){
#$id = $node.id
#Write-Output($id)
#$description = $node.description
#$mid_value = $id.replace('
$new_label = "$RESX('Ebr.Crdm.Store.2_0',"+$id+",'XXXXXX')"
$node.label = $new_label
}
Write-Output($node.label)
}
它出现在调试我的代码时,我无法正确更新标签。有人可以建议最好的方法来改变它。在运行代码时,我没有看到 $node.label 打印出来。
问题:
- 在我的输出中看不到 $node.description 打印(已解决)
- 在 IF 块中更新 $node.label 后看不到打印出来
【问题讨论】:
-
@vonPryz 为了更清楚,我把它说得很短。如果您有任何建议,请查看并告诉我
-
看来我没有正确引用描述变量,这很可能是导致问题的原因
-
@JamesZ 感谢您编辑我的问题。编辑看起来非常好
-
我想出正确的引用方式是:"$nodes = $xml.Searchable.Fields.Field.LeftExpression.Field" 这意味着我必须对循环进行更改
标签: .net xml powershell loops