错误:"'<'是一个意外的令牌。预期的令牌是&#39 ;;'

时间:2017-06-25 18:12:34

标签: xml powershell

我试图将XML附加到节点

            #Insert Data into template
            foreach ($listXml in $webConfig.Lists.List)
            {
                $dataXml =""

                if ($listXml.IncludeData)
                {
                    $list = Get-PnPList -Web $web -Identity $listXml.Name       
                    $list.ParentWeb.Context.Load($list.Fields)
                    $list.ParentWeb.Context.ExecuteQuery()

                    Write-Host $list.Title
                    $listItems = Get-PnPListItem -List $list.Title

                    $dataXml += '<pnp:DataRows xmlns:pnp="http://schemas.dev.office.com/PnP/2016/05/ProvisioningSchema">'


                    foreach ($listItem in $listItems)
                    {

                       $dataXml += "<pnp:DataRow>"


                       foreach($fieldValue in $listItem.FieldValues.GetEnumerator())
                       {

                            $fieldName = $fieldValue.Key #($list.Fields | Where {$_.InternalName -eq $fieldValue.Key}).Title

                            if ($fieldName -notin "ServerRedirectedEmbedUri", "ContentTypeID", "_ModerationComments", "File_x0020_Type", "ID","Author","Created","Editor","Modified By","_HasCopyDestinations",
            "_CopySource", "owshiddenversion", "WorkflowVersion", "_UIVersion", "_UIVersionString", "Attachments", "FileRef", "FileDirRef", "_ModerationStatus", "InstanceID", "WorkflowInstanceID", "Order", "GUID", "Workflow Instance ID", "URL Path",
            "Path", "Modified", "Created", "Item Type", "Sort Type", "Name",  "Unique Id",  "Client Id",  "ProgId", "ScopeId", "Property Bag", "_Level", "_IsCurrentVersion","ItemChildCount",
            "FolderChildCount","Restricted", "OriginatorId", 'ContentVersion;', "_ComplianceFlags", "_ComplianceTag", "_ComplianceTagWrittenTime", "_ComplianceTagUserId",
            "AccessPolicy", "AppAuthor","AppEditor", "SMTotalSize", "SMLastModifiedDate", "SMTotalFileStreamSize", "SMTotalFileCount", "Last_x0020_Modified", "Created_x0020_Date", "FSObjType", "SortBehavior",  "FileLeafRef", "UniqueId", "SyncClientId", "MetaInfo",  "NoExecute","ContentVersion" )
                            {

                                if ($fieldName -ne "" -and $fieldName -ne $null)
                                {                                        

                                    $dataXml += "<pnp:DataValue FieldName=""$fieldName"" >" 
                                    $dataXml += $fieldValue.Value
                                    $dataXml += "</pnp:DataValue>"

                                }
                            }

                       }

                       $dataXml += "</pnp:DataRow>"

                    }

                    $dataXml += "</pnp:DataRows>"

                }

                $filename = $FILENAME
                [xml]$templateXml = Get-Content "$path\Templates\$template\$environmentName\$filename"
                Write-Host $list.Title -ForegroundColor yellow
                $xmlList = $templateXml.Provisioning.Templates.ProvisioningTemplate.Lists.ListInstance | where { $_.Title -eq $list.Title}

                clear-content "templates/log.txt"
                add-content "templates/log.txt" $dataXml
                [xml]$XmlData = $dataXml

                $xmlList.AppendChild([System.Xml.XmlNode]$templateXml.ImportNode($XmlData.DataRows,$true))

                $templateXml.Save("$path\Templates\$template\$environmentName\$filename")

我收到此错误,但我不确定如何解决Powershell问题: enter image description here

1 个答案:

答案 0 :(得分:1)

我不知道Powershell所以我不能确定,但​​我可以做出很好的猜测。唯一的地方“;”在XML中很重要的是终止像&nbsp;这样的实体引用,所以我怀疑你的“XML”包含的&不是格式良好的实体引用的开头。看看你的代码,你是通过连接某些输入中找到的字符串值来构造XML,并且你似乎没有尝试转义特殊字符,所以如果输入字符串中有&,你就是不要将其作为&amp;转义。