1.17.20. fejezet, XML fájlok kezelése

File betöltése

$xml = [xml] (Get-Content c:\temp\test.xml)

Elemek elérése

($xml.data.FirstChild).book[0].price
#Az ISE kiértékeli az XML file-t, és segít az elemek megtalálásában

XPath kifejezés kiértékelése

$query = "/data/store/book[@price>10]/title"
$nodes = Select-Xml -XPath $query -Path C:\temp\teszt.xml | Select -Expand Node
$nodes

Névtér használata:

$query = "//d:data/store/book[@price>10]/title"
$ns = @{d="defiant-namespace"}
$nodes = Select-Xml -XPath $query -Path C:\temp\teszt.xml -Namespace $ns| Select -Expand Node
$nodes

XML tartalom:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="teszt.xsl"?>
<d:data xmlns:d="defiant-namespace" d:mi="23">
    <store d:mi="22">
        <book price="12.99" d:price="Number" d:mi="4">
            <title d:constr="String" d:mi="1">Sword of Honour</title>
            <category d:constr="String" d:mi="2">fiction</category>
            <author d:constr="String" d:mi="3">Evelyn Waugh</author>
        </book>
        <book price="8.99" d:price="Number" d:mi="9">
            <title d:constr="String" d:mi="5">Moby Dick</title>
            <category d:constr="String" d:mi="6">fiction</category>
            <author d:constr="String" d:mi="7">Herman Melville</author>
            <isbn d:constr="String" d:mi="8">0-553-21311-3</isbn>
        </book>
        <book price="8.95" d:price="Number" d:mi="13">
            <title d:constr="String" d:mi="10">50</title>
            <category d:constr="String" d:mi="11">reference</category>
            <author d:constr="String" d:mi="12">Nigel Rees</author>
        </book>
        <book price="22.99" d:price="Number" d:mi="18">
            <title d:constr="String" d:mi="14">The Lord of the Rings</title>
            <category d:constr="String" d:mi="15">fiction</category>
            <author d:constr="String" d:mi="16">J. R. R. Tolkien</author>
            <isbn d:constr="String" d:mi="17">0-395-19395-8</isbn>
        </book>
        <bicycle price="19.95" d:price="Number" d:mi="21">
            <brand d:constr="String" d:mi="19">Cannondale</brand>
            <color d:constr="String" d:mi="20">red</color>
        </bicycle>
    </store>
</d:data>

Objektum XML-é konvertálás

$xml = Get-Process | ConvertTo-Xml
$xml.Save("c:\temp\out.xml")
 
#pipe alternatíva
Get-Process | ConvertTo-Xml -As String > c:\temp\processes.xml

Hash táblából xml

$array = @("item1", "item2", "item3")
$array | ConvertTo-Xml -As String > c:\temp\out.xml

Eredmény

<?xml version="1.0" encoding="utf-8"?>
<Objects>
  <Object Type="System.String">item1</Object>
  <Object Type="System.String">item2</Object>
  <Object Type="System.String">item3</Object>
</Objects>

XML módosításhoz forrás phonebook.xml:

<AddressBook>
  <Person contactType="Personal">
    <Name>Lee</Name>
    <Phone type="home">555-1212</Phone>
    <Phone type="work">555-1213</Phone>
  </Person>
  <Person contactType="Business">
    <Name>Ariel</Name>
    <Phone>555-1234</Phone>
  </Person>
</AddressBook>

$phoneBook = [xml] (Get-Content C:\temp\phonebook.xml)
$person = $phoneBook.AddressBook.Person[0]
## Change the text part of the information
## and the type (which was an attribute) 
$person.Phone[0]."#text" = "555-1214"
$person.Phone[0].type = "mobile"
## Add a new phone entry
$newNumber = [xml] '<Phone type="home">555-1215</Phone>'
$newNode = $phoneBook.ImportNode($newNumber.Phone, $true)
[void] $person.AppendChild($newNode)
## Save the file to disk 
$phoneBook.Save("C:\temp\phonebook.xml")

Hash tábla exportálása file-ba:

$favorites = @{} 
$favorites["temp"] = "c:\temp"
$favorites["music"] = "h:\lee\my music"
$favorites | Export-CliXml c:\temp\favorites.clixml
$favorites = $null

Export eredménye:

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Collections.Hashtable</T>
      <T>System.Object</T>
    </TN>
    <DCT>
      <En>
        <S N="Key">music</S>
        <S N="Value">h:\lee\my music</S>
      </En>
      <En>
        <S N="Key">temp</S>
        <S N="Value">c:\temp</S>
      </En>
    </DCT>
  </Obj>
</Objs>

Importálás:

$favorites = Import-CliXml c:\temp\favorites.clixml