将字符串解析为 XML Powershell

Parse string into XML Powershell

我正在尝试将字符串解析为 SharePoint Lob 中的 XML 字符串

我要解析的字符串:

f#^)X&9uk7u%

和指定了转义字符的字符串

f#^)X&9uk7u%

解析 Powershell 时抛出错误:

Unable to parse specified property into type System.String

导致问题的字符是 &。如果我删除 &,我可以毫无问题地解析。

任何想法都会很棒。

代码如下

XML具有如下属性

<LobSystemInstance Name="MyLobInstance" DefaultDisplayName="MyLobInstance">
<Properties>
    <Property Name="Property1" Type="System.Boolean">true</Property>
    <Property Name="Property2" Type="System.String">{Property1}</Property>
    <Property Name="Property3" Type="System.String">{Property3}</Property>
    <Property Name="Property4" Type="System.String">{Property4}</Property>
    <Property Name="Property5" Type="System.String">{Property5}</Property>
    <Property Name="Property6" Type="System.String">{Property6}</Property>
    <Property Name="Property7" Type="System.String">{Property7}</Property>
    <Property Name="Property8" Type="System.String">{Property8}</Property>
    <Property Name="Property9" Type="System.String">{Property9}</Property>
    <Property Name="Property10" Type="System.String">{Property10}</Property>
    <Property Name="Property11" Type="System.String">{Property11}</Property>
    <Property Name="Property12" Type="System.String">{Property12}</Property>
   <Property Name="Property13" Type="System.String">{Property13}</Property>
</Properties>

powershell命令如下

//-- Get the BDC Schema as a string

$bdcModelSchema = (Get-Content $bdcModelFile.FullName) | Out-String 

//Replace the strings with desired values.

$bdcModelSchema = $bdcModelSchema.Replace("{Property1}", $Property2)
$bdcModelSchema = $bdcModelSchema.Replace("{Property2}", f#^)X&9uk7u% | Out-String)
$bdcModelSchema = $bdcModelSchema.Replace("{Property3}", f#^)X&9uk7u%)
$bdcModelSchema = $bdcModelSchema.Replace("{Property4}", $Property4)
$bdcModelSchema = $bdcModelSchema.Replace("{Property5}", "Property5")
$bdcModelSchema = $bdcModelSchema.Replace("{Property6}",$Property6)
$bdcModelSchema = $bdcModelSchema.Replace("{Property7}", $Property7)
$bdcModelSchema = $bdcModelSchema.Replace("{Property8}", $Property8)
$bdcModelSchema = $bdcModelSchema.Replace("{Property9}", $Property9)
$bdcModelSchema = $bdcModelSchema.Replace("{Property10}", $Property10)
$bdcModelSchema = $bdcModelSchema.Replace("{Property11}", $Property11)
$bdcModelSchema = $bdcModelSchema.Replace("{Property12}", $Property12)
$bdcModelSchema = $bdcModelSchema.Replace("{Property13}", ($Property13))

要在您的代码中使 r$!)X&8ul7u$ 成为一个字符串,您必须将它放在单引号或双引号之间。使用双引号时,必须转义 $ 个字符。

$bdcModelSchema = $bdcModelSchema.Replace("{Property2}", 'r$!)X&8ul7u$')

$bdcModelSchema = $bdcModelSchema.Replace("{Property2}", "r`$!)X&8ul7u`$")

不行,因为在 XML 中 & 是元字符。

为了解决这个问题,字符串应该定义为

$bdcModelSchema = $bdcModelSchema.Replace("{Property2}", 'x$*)X&amp;7hl9u#')