用于解析XML的嵌套foreach语句

时间:2013-04-01 17:30:55

标签: sql xml parsing powershell

我的xml看起来类似于以下

<Settings>
    <CentralOnly>
        <DateLists>
            <DateList Name="name1" ID="1">
                <Date>*-01-01</Date>
                <Date>*-12-25</Date>
                <Date>2007-05-28</Date>
                <Date>2007-09-03</Date>
            </DateList>
            <DateList Name="name2" ID="2">
                <Date>2008-11-11</Date>
                <Date>2008-11-27</Date>
            </DateList>
            <DateList Name="name3" ID="3">
                <Date>2008-03-21</Date>
                <Date>2010-04-02</Date>
            </DateList>
        </DateLists>
    </CentralOnly>
</Settings>

最终目标是将其放入sql server 2008 r2数据库。我目前的这种做法(很差)是将每一列写入一个单独的文本文件

dateID.txt   |  specificDate.txt
    1        |     *-01-01
    1        |     *-12-25
    1        |     2007-05-28
    1        |     2007-09-03
    2        |     2008-11-11
    2        |     2008-11-27
    3        |     2008-03-21
    2        |     2010-04-02

我认为下面的代码是以正确的方式进行的(它还没有用)。但是我必须有一种方法可以使用一个文件,也许是一个csv?

set-executionpolicy unrestricted
[xml]$config = Get-Content c:\path\config.xml
new-item c:\path\dlDays.txt -type file -force
new-item c:\path\dlDLID.txt -type file -force
$xml = $config.settings.DateLists.DateList | % $_.DateList{
    $dateID = $_.id
    $eachDate = $_.date | % $_.date{
        $specificDate = $_.date
        add-content c:\path\dlDays.txt $specificDate
        add-content c:\path\dlDLID.txt $dateID
    }
}

2 个答案:

答案 0 :(得分:3)

#12: Output Objects

我使用了你的XML文件,而不是输出两个文件,我创建了一个对象数组。然后我将这些对象传送到Export-Csv。

[xml]$config = Get-Content date.xml
$newdlist = @()
foreach($dlist in $config.Settings.CentralOnly.DateLists.DateList){
    $dateID = $dlist.ID
    foreach($date in $dlist.Date){
        $newdlist += @(New-Object -TypeName psobject -Property @{'ID'=$dateID;'Date'=$date})
    }
}
$newdlist | Export-Csv -Path dat.csv

现在,如果您想再次使用$ newdlist,只需使用Import-Csv导入Csv文件。

PS C:\> $data = Import-Csv date.csv
PS C:\> $data

ID                                                          Date
--                                                          ----
1                                                           *-01-01
1                                                           *-12-25
1                                                           2007-05-28
1                                                           2007-09-03
2                                                           2008-11-11
2                                                           2008-11-27
3                                                           2008-03-21
3                                                           2010-04-02

我不知道如何在PowerShell中使用SQL,但现在更容易使用数据。

$data | % {"INSERT '$($_.Date)' INTO 'Id' Where 'Id'='$($_.ID)'"}

更多

CSV文件如下所示:

#TYPE System.Management.Automation.PSCustomObject
"ID","Date"
"1","*-01-01"
"1","*-12-25"
"1","2007-05-28"
"1","2007-09-03"
"2","2008-11-11"
"2","2008-11-27"
"3","2008-03-21"
"3","2010-04-02"

答案 1 :(得分:1)

以下是我用来生成csv文件的内容:

[xml]$xml = Get-Content -path "c:\temp\datedata.xml"

$nodes = $xml.Settings.CentralOnly.DateLists.DateList

$array = @()

foreach ($node in $nodes) {

    $id = $node.ID

    foreach ($date in $node.Date) {

        $entry = New-Object PSObject -property @{
            ID = $id
            Date = $date
        }

        $array += $entry
    }

}

$array | Export-Csv -Path 'c:\Temp\data.csv' -Delimiter ',' -noTypeInformation

注意:您需要使用-noTypeInformation从csv文件中排除类型。