问题
I have created a function that reads a directory listing and then the files contents for CNC program names/numbers. Each file have multiple program numbers which i have separated by "," and are passed on as a obj (see code below). I want to export this to XML to upload to a database having each of the program names in a seperate entry. Whats the best/easiest method to achieve this?
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name FullFileName -Value $CNCProgFileName
$obj | Add-Member -MemberType NoteProperty -Name ProgramName -Value $ProgramName
回答1:
Creating a custom object like you are doing is good. I suggest creating a property that is an array so you can associate the many things in the array to one object.
$myListOfStuff = @()
$myListOfStuff += "Stuff"
$myListOfStuff += "More stuff"
$obj = New-Object PsObject @{
FullFileName = $CNCProgFileName
ProgramName = $ProgramName
ListOfStuff = $myListOfStuff
}
You can see your list of stuff with Select-Object
with -ExpandProperty
like so: $obj | select -expand ListOfStuff
.
回答2:
In PowreShell 2.0 you can use the ConvertTo-Xml
cmdlet to produce XML from your objects.
来源:https://stackoverflow.com/questions/9198622/powershell-and-one-to-many-relationships-objects