将字符串解析为XML Powershell

时间:2015-03-19 08:42:25

标签: powershell sharepoint-2010 bcs

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

我试图解析的字符串:

  

F#^)X&安培; 9uk7u%

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

  

F#^)X&安培; 9uk7u%

解析Powershell时会抛出错误:

  

无法将指定的属性解析为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))

2 个答案:

答案 0 :(得分:0)

要在代码中创建r$!)X&8ul7u$字符串,必须将其放在单引号或双引号之间。使用双引号时,必须转义$个字符。

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

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

答案 1 :(得分:0)

无效,因为在XML中&是一个元字符。

要解决这个问题,应将字符串定义为

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