Bonjour, ici je vous présente comment enregistrer un xml et comment le lire.
Sommaire
Petite interface pour les tests:

Codes:
Pour créer le chemin et le nom du XML
$xmlpath = $PSScriptRoot + "\" +$script.Substring(0,$script.Length-4)+ ".xml"
Fonction de création du XML
function CreateXMLGUI {
if (test-path $xmlpath) { Remove-item $xmlpath -Force -Recurse }
# Set The Formatting
$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true
$xmlsettings.IndentChars = " "
# Set the File Name Create The Document
$XmlWriter = [System.XML.XmlWriter]::Create($xmlpath, $xmlsettings)
# Write the XML Decleration and set the XSL
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='style.xsl'")
$ValueItem1 = $Txt_Item1.Text
$ValueItem2 = $Txt_Item2.Text
# Start the Root Element
$xmlWriter.WriteStartElement("XML-Work") # <-- Start <Root>
$xmlWriter.WriteStartElement("Text") # <-- Start <Object>
$xmlWriter.WriteElementString("ValueItem1","$ValueItem1")
$xmlWriter.WriteElementString("ValueItem2","$ValueItem2")
$xmlWriter.WriteEndElement() # <-- End <Object>
$xmlWriter.WriteEndElement() # <-- End <Root>
# End, Finalize and close the XML Document
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
}
Fonction de lecture du XML
function Read-XML
{
if (test-path $xmlpath)
{
Write-Host "Read XML: $xmlpath"
[xml]$xml = get-content ("$xmlpath")
foreach ($valeurxml in $xml.'XML-Work')
{
$Txt_Item1.Text = $valeurxml.Text.ValueItem1
$Txt_Item2.Text = $valeurxml.Text.ValueItem2
}
}
else { Write-Host "no xml found" }
}
Fichiers
Voici la forme du fichier .xml généré

Je vous laisse le script PS1 en téléchargement.