我在下面输入文字,并希望从中提取一些数据:
<UserInputs>
<UserInput Question="Service Type" Answer="User Account Management" Type="string" />
<UserInput Question="User Account Management" Answer="New User Account" Type="string" />
<UserInput Question="User Account - First Name " Answer="John" Type="string" />
<UserInput Question="User Account - Last Name" Answer="Snow" Type="string" />
<UserInput Question="User Account - Title" Answer="Officer" Type="string" />
<UserInput Question="User Account - Department" Answer="IT" Type="string" />
<UserInput Question="User Account - Division" Answer="Infratructure" Type="string" />
<UserInput Question="User Account - Mobile" Answer="+962799999993" Type="string" />
<UserInput Question="User Account - IP Phone Extension" Answer="8879" Type="string" />
<UserInput Question="User Account - Direct Manager" Answer="<Values Count="1"><Value DisplayName="Jack" Id="8c75d26a-2eaf-7e45-9139-178b88c42d6d"/></Values>" Type="System.SupportingItem.PortalControl.InstancePicker" />
<UserInput Question="Urgency" Answer="b02d9277-a9fe-86f1-e95e-0ba8cd4fd075" Type="enum" />
</UserInputs>
我尝试过类似split
之类的方法,但是它不起作用(获取一些值Null
),您能帮我吗?
答案 0 :(得分:0)
您可以为此使用Powershells XML功能
使用[xml]$xml = Get-Content -Path '<Path to the xml file>' -Raw
读入文本文件
在这里,我使用“ here-string”来伪造
[xml]$xml = @"
<UserInputs>
<UserInput Question="Service Type" Answer="User Account Management" Type="string" />
<UserInput Question="User Account Management" Answer="New User Account" Type="string" />
<UserInput Question="User Account - First Name " Answer="John" Type="string" />
<UserInput Question="User Account - Last Name" Answer="Snow" Type="string" />
<UserInput Question="User Account - Title" Answer="Officer" Type="string" />
<UserInput Question="User Account - Department" Answer="IT" Type="string" />
<UserInput Question="User Account - Division" Answer="Infratructure" Type="string" />
<UserInput Question="User Account - Mobile" Answer="+962799999993" Type="string" />
<UserInput Question="User Account - IP Phone Extension" Answer="8879" Type="string" />
<UserInput Question="User Account - Direct Manager" Answer="<Values Count="1"><Value DisplayName="Jack" Id="8c75d26a-2eaf-7e45-9139-178b88c42d6d"/></Values>" Type="System.SupportingItem.PortalControl.InstancePicker" />
<UserInput Question="Urgency" Answer="b02d9277-a9fe-86f1-e95e-0ba8cd4fd075" Type="enum" />
</UserInputs>
"@
# The 'Direct Manager' has two properties we need to filter out.
$manager = ($xml.UserInputs.UserInput | ? { $_.Question -match 'Direct Manager\s*$' }).Answer
$managerName = $managerId = 'Unknown'
if ($manager -match 'DisplayName=(?:"|")(?<name>[^&"]+).+Id=(?:"|")(?<id>[^&"]+)') {
$managerName = $matches['name']
$managerId = $matches['id']
}
# Create an object of the info for output
[PSCustomObject]@{
'First tName' = ($xml.UserInputs.UserInput | Where-Object { $_.Question -match 'First Name\s*$' }).Answer
'Last Name' = ($xml.UserInputs.UserInput | Where-Object { $_.Question -match 'Last Name\s*$' }).Answer
'Title' = ($xml.UserInputs.UserInput | Where-Object { $_.Question -match 'Title\s*$' }).Answer
'Department' = ($xml.UserInputs.UserInput | Where-Object { $_.Question -match 'Department\s*$' }).Answer
'Division' = ($xml.UserInputs.UserInput | Where-Object { $_.Question -match 'Division\s*$' }).Answer
'Mobile' = ($xml.UserInputs.UserInput | Where-Object { $_.Question -match 'Mobile\s*$' }).Answer
'IP Phone Extension' = ($xml.UserInputs.UserInput | Where-Object { $_.Question -match 'IP Phone Extension\s*$' }).Answer
'Direct Manager Name' = $managerName
'Direct Manager Id' = $managerId
}
这将产生:
First Name : John
Last Name : Snow
Title : Officer
Department : IT
Division : Infratructure
Mobile : +962799999993
IP Phone Extension : 8879
Direct Manager Name : Jack
Direct Manager Id : 8c75d26a-2eaf-7e45-9139-178b88c42d6d
希望有帮助
答案 1 :(得分:0)
我确实解决了以下问题:
a