【问题标题】:Updating XML element and attribute value using Powershell使用 Powershell 更新 XML 元素和属性值
【发布时间】:2020-06-10 20:57:32
【问题描述】:

这是我正在使用的 XML。这是在 sqlproj 文件中,并且希望将 ArtifactReference 的 Include 属性和节点元素 HintPath 中的值“..\cwdb.dacpac”更新为新值。

<ItemGroup>
    <ArtifactReference Include="$(DacPacRootPath)\Extensions\Microsoft\SQLDB\Extensions\SqlServer\AzureV12\SqlSchemas\master.dacpac">
      <HintPath>$(DacPacRootPath)\Extensions\Microsoft\SQLDB\Extensions\SqlServer\AzureV12\SqlSchemas\master.dacpac</HintPath>
      <SuppressMissingDependenciesErrors>True</SuppressMissingDependenciesErrors>
      <DatabaseVariableLiteralValue>master</DatabaseVariableLiteralValue>
    </ArtifactReference>
    <ArtifactReference Include="..\cwdb.dacpac">
      <HintPath>..\cwdb.dacpac</HintPath>
      <SuppressMissingDependenciesErrors>True</SuppressMissingDependenciesErrors>
      <DatabaseVariableLiteralValue>cwdb</DatabaseVariableLiteralValue>
    </ArtifactReference>
  </ItemGroup>

以下部分代码基于此博客https://blogs.like10.com/2014/06/17/versioning-your-sql-server-database-using-team-build-and-release-management/,但它不会更新值。在对函数 Set-XMlElementsTextValue 的调用中,传递元素路径 HintPath 以更新其值。如何更改 Include 属性值?

function Get-XmlNode([ xml ]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = “”, [string]$NodeSeparatorCharacter = ‘.’)
{
# If a Namespace URI was not given, use the Xml document’s default namespace.
if ([string]::IsNullOrEmpty($NamespaceURI))
{
$NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI
}
# In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
$xmlNsManager.AddNamespace(“ns”, $NamespaceURI)
$fullyQualifiedNodePath = “/ns:$($NodePath.Replace($($NodeSeparatorCharacter), ‘/ns:’))”
$node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)

return $node
}
function Set-XMlElementsTextValue([ xml ]$XmlDocument, [string]$ElementPath, [string]$TextValue)
{
    $node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath
    # If the node exists, update its value.
    if ($node)
    {
         $node.InnerText = $TextValue
    }
}

$path = "C:\Shared\DACPAC\"
$NewText = $path + "cwdb.dacpac"
$files = gci $env:BUILD_SOURCESDIRECTORY -recurse |
         ?{ $_.Extension -eq ".sqlproj" } |
         foreach { gci -Path $_.FullName -Recurse -include *.sqlproj }

if($files)
{
        foreach ($file in $files) {
            [xml]$fileContent = Get-Content($file)
             attrib $file -r
            #Read in the file contents, update the element's value, and save the file.
             Set-XMlElementsTextValue -XmlDocument $fileContent -ElementPath "Project.ItemGroup.ArtifactReference.HintPath" -TextValue $NewText
            $fileContent.Save($file)
           }

}
else
{
        Write-Output "Found no *.sqlproj files."
}

【问题讨论】:

    标签: xml powershell


    【解决方案1】:

    我更新了我的代码并删除了函数并将命名空间包含在主代码中。这可能不是最优雅的解决方案,但这里是修改后的代码:

    if($files)
    {
            foreach ($file in $files) {
                [xml]$fileContent = Get-Content($file)
                 attrib $file -r
    
                $XPath = "/ns:Project/ns:ItemGroup/ns:ArtifactReference[@Include='..\cwdb.dacpac']"          
                $namespace = $fileContent.DocumentElement.NamespaceURI
                $ns = New-Object System.Xml.XmlNamespaceManager($fileContent.NameTable)
                $ns.AddNamespace("ns", $namespace)
                $artifacts = $fileContent.SelectSingleNode($XPath, $ns)
                # update HintPath node value
                $artifacts.SelectSingleNode("./ns:HintPath", $ns)."#text" = $NewText
                Foreach ($artifact in $artifacts) 
                {   # update Include attribute value
                    $artifact.SetAttribute("Include", $NewText) 
                }
    
                $fileContent.Save($file)
    
                Write-Output "$file.FullName = update applied"
                }
    
    
    }
    else
    {
            Write-Output "Found no *.sqlproj files."
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 2021-12-13
      • 1970-01-01
      • 2020-05-30
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多