1

さまざまな要素を持つ xml ファイルがいくつかあります。各要素を繰り返し処理して値を取得するコードを c# または PowerShell で書きたいと思います。他の xml ファイルで使用できるように、十分に一般的なものにしたいと考えています。すべての xml ファイルの形式は同じですが、要素の名前が異なります。

どうもありがとうございました。

<root>
  <list>
       <FirstName>Abc</FirstName>
       <LastName>LT</LastName>
       <Occupatoin>Eng</Occupation>
       <BirthDate></BirthDate>
      ...
 </list>
</root>

----------------------
XML file 2

<root>
  <Trainings>
       <Java>Ab</Java>
       <NET>b</NET>
       <SQL>c</SQL>
       <Powershell>d</Powershell>
      ...
 </Trainings>
</root>

上記の xml ファイル内の要素の繰り返しのベースとなる別の xml が導入されました。

以下の各要素の属性に基づいて、上記の要素の値を取得します。

<root>
    <Element Name="Firstname />
    <Element Name="Lastname" />
    <Element Name="Occupation" />
    <Element Name="Java" />
    <Element Name="Net" />
...
</root>


[System.Xml.XmlDocument] $xdLists = new-object System.Xml.XmlDocument
[System.Xml.XmlDocument] $xdMig = new-object System.Xml.XmlDocument
$listfile = "C:\PowershellFiles\XmlLists.xml"
$xdLists.load($listfile)

$xdNodes= $xdLists.selectnodes("//DestinationLists/DestinationList")
$migCols = $xdLists.selectnodes("//MigrationColumns/Columns/Column")

#LOOP 1-----------------------------------------
foreach($xnode in $xdNodes)
{
    Write-Host $xnode.Attributes.GetNamedItem("MigrationFile").Value
    $destLists = $xnode.Attributes.GetNamedItem("Name").Value
    $migfiles = $xnode.Attributes.GetNamedItem("MigrationFile").Value
    
    
    Write-Host $destLists
    
    #Check if the xml file to read from exists
    
    if($migFiles -ne $null)
    {
            $xdMig.Load($migfiles)
        
            $spSite = Get-SPSite "http://sp2010:100" 
            $spWeb = $spSite.OpenWeb()

            $list = $spWeb.Lists[$destLists]

            foreach($nCol in $migCols)
            {
                $destListCol =  $nCol.Attributes.GetNamedItem("DestList").Value
                $sourcCol =  $nCol.Attributes.GetNamedItem("SourceCol").Value

#               Write-Host $col " - " $list.Title

                if($destListCol -eq $list.Title)
                {
                    Write-Host $destListCol " - " $list.Title " - Source Column: " $sourcCol  -ForegroundColor Green
                    Write-Host
                    
                    # ----------------------- time to search the exported lists --------------------
                    Write-Host "Search the exported list for the right column" -ForegroundColor  DarkYellow
                    
                    if($xdMig.DocumentElement -ne $null)
                        {
                            $xnList = $xdMig.DocumentElement.ChildNodes

                    #           LOOP 2----------------------------------------
                            Write-Host $xnList.Count " items found" -ForegroundColor Red
                            foreach($xn in $xnList)
                            {
                                Write-Host $xn.Name -ForegroundColor Red
                                
                                $nList = $xdMig.SelectNodes("//"+$xn.Name)
                                $lItem = $list.Items.Add()

                                foreach($n in $migCols)
                                  {
                                  
                                    if($n.Attributes -ne $null)
                                    {
                                        $sourceCol = $n.Attributes.GetNamedItem("SourceCol").Value
                                        $destCol = $n.Attributes.GetNamedItem("DestCol").Value
                                        $destList = $n.Attributes.GetNamedItem("DestList").Value
                                        
                                        Write-Host "Dest Col: " $destCol  "-  Sour Col: " $xn[$sourceCol].Name 
                                        Write-Host $destList -ForegroundColor Red

                                        if($list.Title -eq $destList)
                                        {
                                            if($xn[$sourceCol] -ne $null )
                                            {
                                                if($list.Fields[$destCol] -ne $null)
                                                {
                                                    $lItem[$destCol] = $xn[$sourceCol].InnerText    
                                                }
                                                        
                                            }else
                                            {
                                                Write-Host   $sourceCol " was not matched" -ForegroundColor Yellow
                                            }
                                        }
                                     }
                                  }
                                  $lItem.Update()
                                  Write-Host "-----------------------------------"
                            }

                        }
                }
            }
    }
}
4

2 に答える 2

2

を組み合わせることができます。XML エラーを修正Element.Nameし、データ ファイルと同じ大文字と小文字になるように名前を変更したとします。

your_file.xml固定コンテンツ:

<root> 
  <list> 
       <FirstName>Abc</FirstName> 
       <LastName>LT</LastName> 
       <Occupation>Eng</Occupation> 
       <BirthDate></BirthDate> 
      ... 
 </list> 
</root>

index_file.xml固定コンテンツ:

<root> 
    <Element Name="FirstName" /> 
    <Element Name="LastName" /> 
    <Element Name="Occupation" /> 
    <Element Name="Java" /> 
    <Element Name="NET" /> 
... 
</root>

PoSHマッピングするには:

$xmlIndex = [xml](gc ".\index_file.xml")
$xml = [xml](gc ".\your_file.xml")
$allValues = @{}; 

$xmlIndex.SelectNodes("//Element/@Name") | 
    %{ $nodeName = $_."#text"; $xml.SelectNodes("//$nodeName/text()") } |
    % { $allvalues[$_.ParentNode.ToString()] = $_.Value };

$allValues | ft

出力が続きます

Name                           Value
----                           -----
Occupation                     Eng
FirstName                      Abc
LastName                       LT

Name                           Value
----                           -----
NET                            b
Java                           Ab
于 2012-07-28T15:35:07.650 に答える