powershell xml中的正斜杠

时间:2014-02-14 15:51:08

标签: xml powershell

我有一个大的xml文件,我正在尝试从中获取一些数据。我想要的XML中有一节:

<ContentItem>
<Variant variantCulture="en-US">
  <Field name="NavigationListItem/Filter/0/#Name" type="String">my bill</Field>
  <Field name="NavigationListItem/Filter/1/#Name" type="String">Trending topics</Field>
  <Field name="NavigationListItem/Filter/2/#Name" type="String">Manage Payment Options</Field>
  <Field name="NavigationListItem/Filter/0/Key" type="String">my bill</Field>
  <Field name="NavigationListItem/Filter/0/Title" type="String">my bill</Field>
  <Field name="NavigationListItem/Filter/0/Description" type="String"></Field>
  <Field name="NavigationListItem/Filter/1/Key" type="String">Trending topics</Field>
  <Field name="NavigationListItem/Filter/1/Title" type="String">Trending topics</Field>
  <Field name="NavigationListItem/Filter/1/Description" type="String"></Field>
  <Field name="NavigationListItem/Filter/2/Key" type="String">Manage Payment Options</Field>
  <Field name="NavigationListItem/Filter/2/Title" type="String">Manage Payment Options</Field>
  <Field name="NavigationListItem/Filter/2/Description" type="String"></Field>

到目前为止,我的代码如下。这重复了几种variantCultures。到目前为止,我的代码可以循环遍历所有的variantCultures并打印所有字段(不仅仅是我给的代码片段)。我希望能够在最后打印出这样的东西:

过滤器名称 过滤密钥 过滤器描述

适用于所有过滤器。我只是在尝试用/中的/来选择字段名称时遇到了麻烦。

Get-ChildItem C:\Users\rasuser\Desktop\import\bill.xml | 
% { 
    $file = [xml](Get-Content $_.fullname)

    $path = $file.ExportedContentItem.path
    $name = $file.ExportedContentItem.name
    $GUID = $file.ExportedContentItem.ID


    $file.ExportedContentItem.ContentItem.Variant | % { 
        $locale = $_.variantCulture         

        $_.Field
    }       

    $out = $path + "/" + $name + "`t" + $GUID

    $out >> ".\outputee.txt"
} 

1 个答案:

答案 0 :(得分:1)

好的,只使用您提供给我们的示例片段保存到XML文件中,请告诉我这是否是您要找的...

$Filters = @()
$file = [xml](Get-Content C:\Users\rasuser\Desktop\import\bill.xml)

    $file.ContentItem.Variant | % { 
        $_.Field|%{$CurrentFilter = [pscustomobject]@{Value=$_.'#Text';Filter=$_.name.Split("/")[2];Key=$_.name.Split("/")[3]}
        $Filters+=$CurrentFilter
        }
        $Groups=@()
        $filters|Group-Object Filter|%{
            $CurrentGroup = [PSCustomObject]@{Filter=$_.Group.Filter[0]}
            $_.Group|%{Add-Member -InputObject $CurrentGroup -MemberType NoteProperty -Name $_.Key -Value $_.Value}
            $Groups+=$CurrentGroup
        }
    }       
    $groups|ft
}