XML to string PowerShell

时间:2018-05-14 17:31:24

标签: xml powershell

I'm trying to output "John Doe" to a string with OuterXml. The final line returns nothing.

<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:New-Hire-Checklists:-myXSD-2016-01-21T15-44-37" solutionVersion="1.1.6.702" productVersion="16.0.0.0" PIVersion="1.0.0.0" href="https://symbiota.sharepoint.com/employeechecklists/NewHires/Forms/template.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>
<?mso-infoPath-file-attachment-present?>
<my:myFields my:Empl-StartDate="2019-01-01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2016-01-21T15:44:37" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
    <my:Empl-Name>John Doe</my:Empl-Name>
</my:myFields>
[xml]$user = Get-Content "C:\Powershell\jd.xml"

$user.myFields | Select-Object Empl-Name

# Output:
#
# Empl-Name      
# ---------      
# John Doe

$name = $user.myFields.OuterXml | Select-Object Empl-Name
$name

# Output:
#
# Empl-Name
# ---------

1 个答案:

答案 0 :(得分:1)

  

尝试将“John Doe”输出到字符串

这样做......

[xml]$doc = @"
<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:New-Hire-Checklists:-myXSD-2016-01-21T15-44-37" solutionVersion="1.1.6.702" productVersion="16.0.0.0" PIVersion="1.0.0.0" href="https://symbiota.sharepoint.com/employeechecklists/NewHires/Forms/template.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>
<?mso-infoPath-file-attachment-present?>
<my:myFields my:Empl-StartDate="2019-01-01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2016-01-21T15:44:37" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
<my:Empl-Name>John Doe</my:Empl-Name>
</my:myFields>
"@

$doc.myFields."Empl-Name"

最后一行将返回一个字符串。这是最简单的方法。