.net - 使用基于关闭条件的 Powershell 更改 XML 中的标签
问题描述
示例 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 打印出来
解决方案
以下是您的 IF 块的编写方式:
if($Resource -match '0'){
$mid_value = $id -replace "_","."
$new_label = "$RESX(Ebr.Crdm.Store.2_0,"+$mid_value+","+$description+")"
$node.label = $new_label
Write-Output("The updated label is"+$node.label)
$b = $node.label
$b -match $regex
$newdata = $Matches[1]
$y = $newdata.split(',')
Write-Output("The first updated element of the node label is"+" "+$y[0])
Write-Output("The second updated element of the node label is"+" "+$y[1])
Write-Output("The third updated element of the node label is"+" "+$y[2])
$insert_query = "INSERT INTO [Secure4].[secure].[Resource]
([ResourceType],[CultureCode],[ResourceKey],[ResourceValue])
VALUES
('"+$y[0]+"','en','"+$y[1]+"','"+$y[2]+"')"
Write-Output($insert_query)
$Resource2 = (Invoke-SQLCmd -query $insert_query -Server $SQLServer)
#write-output($Resource2)
}
推荐阅读
- laravel - Laravel 和 Google 应用引擎 flex 环境。在 nginx 配置中设置缓存控制不起作用
- swift - Swift 以编程方式设置 textview 字体仅适用于键入的新文本而不是所有文本
- angular - 单元测试:如何使用 Angular + Jasmine 模拟窗口对象的 innerWidth 属性?
- java - “未解决的要求:Import-Package: javax.ws.rs”在 Tomcat 上为 Liferay 部署 OSGi 模块时
- csv - 将 CSV 数据 (Windows-1252) 导出到 hive 时出现不可打印字符的问题
- excel - Open file, select sheet and copy it to file with macro
- c++ - 无法调用模板构造函数
- swift - What is the Dart representation of AnyClass in Swift
- amazon-dynamodb - Conditional insert in Dynamodb
- karate - 对于数据驱动的特性,它是否支持正常的数组循环,而不是 json 数组?