我正在使用PowerShell读取从SharePoint表单创建的一些XML文档,并且某些字段似乎允许空值(xsi:nil =" true")并且当找不到任何内容时它将返回"系统.Xml.XmlElement&#34 ;.有没有办法避免这种反应,并获得一个空字符串或没有回来?
$xml = New-Object -TypeName XML
$xml.Load($fullpath)
$sf_r_l5_q1 = $xml.myFields.SF_R_L5_Q1
Write-Host "variable is $sf_r_l5_q1"
<my:SF_R_L5_Q1 xsi:nil="true"/>
variable is System.Xml.XmlElement
经过一些测试后,我添加了一个功能齐全的代码段来尝试显示问题。在Q1和Q3下面的新代码让我头疼。 IsEmpty确实在Q1上工作,但在Q3上没有。 Q1和Q3似乎都是有效的XML格式,我认为两者都可以在InfoPath表单中使用,因此我需要尝试为每个格式做好准备。我可以在Q1格式上使用IsEmpty,但我仍然无法找到使用Q3格式的方法。
$doc = [xml]@'
<xml>
<my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2017-05-05T14:19:13" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" 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:tns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
<my:Q1 xsi:nil="true" />
<my:Q2 />
<my:Q3 xsi:nil="true"></my:Q3>
<my:Q4>true</my:Q4>
</my:myFields>
</xml>
'@
$q1 = $doc.xml.myFields.Q1
$q2 = $doc.xml.myFields.Q2
$q3 = $doc.xml.myFields.Q3
$q4 = $doc.xml.myFields.Q4
$q1value = $doc.xml.myFields.Q1.Value
$q3value = $doc.xml.myFields.Q3.Value
$q4value = $doc.xml.myFields.Q4.Value
$q1_isempty = if ($doc.xml.myFields.Q1.IsEmpty) {"true"} else {"false"}
$q3_isempty = if ($doc.xml.myFields.Q3.IsEmpty) {"true"} else {"false"}
$q3value_blank = if ($doc.xml.myFields.Q3.Value -eq "") {"true"} else {"false"}
$q3_blank = if ($doc.xml.myFields.Q3 -eq "") {"true"} else {"false"}
Write-Host "Q1 $q1"
Write-Host "Q2 $q2"
Write-Host "Q3 $q3"
Write-Host "Q4 $q4"
Write-Host "Q1value $q1value"
Write-Host "Q3value $q3value"
Write-Host "Q4value $q4value"
Write-Host "Q1 IsEmpty $q1_isempty"
Write-Host "Q3 IsEmpty $q3_isempty"
Write-Host "Q3value blank $q3value_blank"
Write-Host "Q3 blank $q3_blank"
Q1 System.Xml.XmlElement
Q2
Q3 System.Xml.XmlElement
Q4 true
Q1value
Q3value
Q4value
Q1 IsEmpty true
Q3 IsEmpty false
Q3value blank false
Q3 blank false