Tôi đang sử dụng PowerShell v3 và Windows PowerShell ISE. Tôi có hàm sau đó hoạt động tốt:Thay đổi PowerShell trả về loại đối tượng
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.
[System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
$xmlNsManager.AddNamespace("ns", $NamespaceURI)
[string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter
# Try and get the node, then return it. Returns $null if the node was not found.
$node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
return $node
}
Bây giờ, tôi sẽ tạo ra một vài chức năng tương tự, vì vậy tôi muốn phá vỡ 3 dòng đầu tiên ra thành một chức năng mới vì vậy mà tôi không cần phải sao chép -Dán họ ở khắp mọi nơi, vì vậy tôi đã làm điều này:
function Get-XmlNamespaceManager([xml]$XmlDocument, [string]$NamespaceURI = "")
{
# 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.
[System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
$xmlNsManager.AddNamespace("ns", $NamespaceURI)
return $xmlNsManager
}
function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
{
[System.Xml.XmlNamespaceManager]$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI
[string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter
# Try and get the node, then return it. Returns $null if the node was not found.
$node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
return $node
}
vấn đề là khi "return $ xmlNsManager" thực hiện các lỗi sau được ném:
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Xml.XmlNamespaceManager".
vì vậy, mặc dù tôi đã đúc một cách rõ ràng các biến $ xmlNsManager của tôi là kiểu System.Xml.XmlNamespaceManager, khi nó được trả về từ hàm Get-XmlNamespaceManager, PowerShell đang chuyển đổi nó thành một mảng Object.
Nếu tôi không rõ ràng truyền giá trị được trả về từ hàm Get-XmlNamespaceManager đến System.Xml.XmlNamespaceManager, thì lỗi sau được ném từ hàm .SelectSingleNode() vì loại dữ liệu sai đang được chuyển vào tham số thứ 2 của hàm.
Cannot find an overload for "SelectSingleNode" and the argument count: "2".
Vì vậy, vì một lý do nào đó PowerShell không duy trì kiểu dữ liệu của biến trả về. Tôi thực sự muốn có được điều này làm việc từ một chức năng để tôi không phải sao chép dán 3 dòng trên khắp nơi. Mọi đề xuất đều được đánh giá cao. Cảm ơn.
Vì vậy, bạn đã làm gì với lệnh ghép ngắn 'Select-Xml' được tích hợp? Việc sử dụng các không gian tên với nó cũng đơn giản như việc truyền cho nó một tiền tố có thể bắt đầu để ánh xạ vùng tên, ví dụ: '$ xml | Select-Xml -XPath '// dns: foo' -namespace @ {dns = 'http: //schema.foo.org'} ' –
Cảm ơn Keith, tôi thực sự đã không nghe nói về lệnh ghép ngắn Select-Xml. Tôi muốn bỏ qua không gian tên Xml tất cả cùng nhau như tôi không quan tâm về nó, nhưng tiếc là .SelectSingleNode() yêu cầu nó, như tôi thảo luận trong bài đăng blog của tôi (http://blog.danskingdom.com/powershell-functions- to-get-an-xml-nút-và-get-và-bộ-một-xml-yếu tố-giá trị-ngay cả khi-phần tử-không-không-đã tồn tại /).Việc sử dụng Select-Xml có giải quyết được vấn đề này không? – deadlydog
'Select-Xml' chỉ gọi hoặc là' SelectSingleNode' hoặc 'SelectNodes' (của XmlDocument) dưới bìa, vì vậy nó vẫn nhạy cảm với các không gian tên XML, đó là lý do tại sao @Keith sử dụng tham số' -namespace'. –