我正在嘗試使用select-xmlPowershell 中的 cmdlet 來查詢我的 .NET 專案中的一些 XAML 檔案。這是我嘗試過的:
select-xml -content (cat $xaml_file_path) -xpath "//GroupBox"
Where$xaml_file_path是一個簡單的字串,其中包含感興趣的 XAML 檔案的檔案路徑。
這會引發錯誤:
Select-Xml:無法驗證引數“內容”上的引數。引數為 null、空或引數集合的元素包含 null 值。提供一個不包含任何空值的集合,然后重試該命令。
我知道 XAML 是有效的,因為它在 Visual Studio 上編譯得很好。所以我想這里可能還有其他事情發生。
有沒有辦法使用 Powershell 查詢 XAML 檔案?如果有怎么辦?
uj5u.com熱心網友回復:
要查詢使用命名空間的 XML (XAML) 檔案,Select-Xml您需要:[1]
通過將自選前綴映射到命名空間 URI(引數)的哈希表,宣告XPath查詢中涉及的任何節點所在的所有命名空間。
-Namespace- 這些自選前綴可以但不必與輸入檔案中的相同,除了默認命名空間( )。
xmlnsxmlns_
- 這些自選前綴可以但不必與輸入檔案中的相同,除了默認命名空間( )。
對 XPath 查詢(引數)中參考的所有節點使用這些自選前綴
-XPath,包括默認(隱含)命名空間中的那些。
一個簡單的例子:
# Create a sample XAML file.
@"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test"
Title="MainWindow" Height="450" Width="500">
<Grid>
<TextBox x:Name="UserInnput" Height="140" TextWrapping="Wrap" VerticalAlignment="Top" AcceptsReturn="True" AcceptsTab="True" Padding="4" VerticalScrollBarVisibility="Auto" />
<Button x:Name="Save" Content="Save" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" IsDefault="True" Height="22" Margin="170,150,0,0" />
</Grid>
</Window>
"@ | Set-Content sample.xml
# Create the hashtable that uses self-chosen names to the URIs
# of those namespaces involved in the XPath query below.
$namespaces = @{
# This is the default namespace, which the elements in the input
# document that have *no* namespace prefix are *implicitly* in.
# You must NOT use 'xmlns' as the prefix.
_ = 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'
# For non-default namespaces, you may choose the same prefixes
# as in the original document.
# Note:
# * This namespace isn't actually used in the query below.
# * However, if the nodes involved in the query do fall into
# this or other namespaces, they must be declared here too,
# and their prefixes must then be used in the query.
x = 'http://schemas.microsoft.com/winfx/2006/xaml'
}
# Query the XAML file, passing the hashtable to -Namespace, and
# using its keys as namespace prefixes; here, '_:' must be used
# to refer to nodes in the default namespace.
Select-Xml -LiteralPath sample.xml -Namespace $namespaces -XPath '//_:TextBox'
如果您想避免處理命名空間:
在
Select-Xml(以及底層System.Xml.XmlDocument.SelectNodes().NET 方法)的背景關系中,避免必須處理命名空間的唯一方法是使用您自己的答案中顯示的*[local-name()='...']解決方法。在PowerShell 改編
[xml]DOM的背景關系中,它為( )的實體添加了“虛擬”屬性:[xml]System.Xml.XmlDocument這些屬性總是以不帶前綴的節點名稱命名;也就是說,命名空間被有效地忽略了。
這很方便并且通常足夠,但它限制了您使用點符號“深入”到檔案中,而不是能夠對檔案運行 XPath查詢;對于后者,您可以使用
.SelectNodes()and.SelectSingleNode()方法,但是,這又需要命名空間管理。[1]使用點表示法的等效示例,基于相同的示例檔案:
# Parse the XML file into an [xml] instance. ($doc = [xml]::new()).Load((Convert-Path sample.xml)) # Drill down to the <TextBox> element. # Note: If the <Grid> element had *multiple* <TextBox> children, # they would *all* be returned, as a System.Xml.XmlElement array. $doc.Window.Grid.TextBox
[1] 命名空間管理的需求類似于直接使用底層System.Xml.XmlDocument.SelectNodes()和System.Xml.XmlDocument.SelectSingleNodes().NET API,盡管在那里構造前綴到 URI 映射表有點麻煩 - 請參閱此答案。
uj5u.com熱心網友回復:
我認為,類似于處理命名空間xmllint可能存在問題。
我發現以下方法確實有效:
(select-xml -path $xaml_file_path -xpath "//*[local-name()='GroupBox']").Node
如果有人知道這樣做的更清潔/更好的方法(即使用-namespace標志),我會很想知道它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520474.html
標籤:电源外壳xml路径PowerShell cmdlet选择-xml
