1

Is there any way to serialize object to xml and back in Powershell?

Step 1: Create custom object

PS C:\Users\User> $custom_obj = [pscustomobject]@{hello='world'}
PS C:\Users\User> $custom_obj

hello
-----
world

Step 2: Convert this object to xml string so we can save it

PS C:\Users\User> $custom_obj_xml = $custom_obj | convertto-xml -as "String"
PS C:\Users\User> $custom_obj_xml
<?xml version="1.0" encoding="utf-8"?>
<Objects>
  <Object Type="System.Management.Automation.PSCustomObject">
    <Property Name="hello" Type="System.String">world</Property>
  </Object>
</Objects>

Step 3: Parse xml string back

PS C:\Users\User> [xml]$parsed_xml_string = $custom_obj_xml
PS C:\Users\User> $parsed_xml_string

xml                            Objects
---                            -------
version="1.0" encoding="utf-8" Objects

How can I get the exact same result as $custom_obj?

Update

I have to work with some legacy code and OS, so ConvertTo-Xml is my only option.

  • Why not use `Export-CliXml` and `Import-CliXml`? – vonPryz Feb 14 '23 at 11:05
  • @vonPryz `Export-CliXml` didn't allow to work without file I/O. I think it's dumb to write xml in the file, read the contents and then delete file. I need to store xml serialized object in memory. – TommyDiligance Feb 14 '23 at 12:39
  • A better alternative is [`PSSerializer`](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.psserializer), which allows you to serialize/deserialize to/from an XML string with a single line of code. See following link. – zett42 Feb 14 '23 at 13:05
  • Does this answer your question? [Is there a way to pass serializable objects to a PowerShell script with start-process?](https://stackoverflow.com/questions/34076478/is-there-a-way-to-pass-serializable-objects-to-a-powershell-script-with-start-pr) – zett42 Feb 14 '23 at 13:07
  • 1
    @zett42 I forgot to mention that i have to work with some legacy code, and ConvertTo-Xml is my only option. It would be a great option to use `PSSerializer` but i can't :( – TommyDiligance Feb 14 '23 at 13:25
  • It would be helpful if you'd explain the bigger picture. Why do you need to serialize and deserialize an object? Maybe we can find an easier way. – zett42 Feb 14 '23 at 14:29

1 Answers1

0

try this

$parsed_xml_string.Objects.Object.Property | ForEach-Object {
Add-Member -InputObject $custom_obj -MemberType NoteProperty -Name $.Name -Value $.'#text'
}
$custom_obj
webapp471
  • 54
  • 6
  • Ok, this works with a simple solution, but what if i have inner object? Something like this: `$custom_obj = [pscustomobject]@{hello='world';inner_obj=[pscustomobject]@{inner_hello='inner_world'}}`. The result would be wrong – TommyDiligance Feb 14 '23 at 08:33
  • So, am I right that powershell can't deserialize it's own xml serialized object? Should i do it by myself and predict all possible complications? – TommyDiligance Feb 14 '23 at 08:36
  • Xml serialization requires the same c# like classes at both client and server so that serialized data can be deserialized. You are treating xml as a psobject which will not work. Your results got the ident line as the xml name and the rest of the xml is appearing in the objects. To work with xml properly in PS you must use Net/Core libraries. – jdweng Feb 14 '23 at 10:09
  • 1
    @jdweng So, am i right that in pure powershell it's impossible to work with xml properly? – TommyDiligance Feb 14 '23 at 12:36
  • Powershell is written in c# and uses Net/Core library. So pure powershell still uses Net/Core. Powershell is an application layer ontop of a Net/Core libraries. So what is pure? – jdweng Feb 14 '23 at 12:45
  • @TommyDiligance nah not correct. PowerShell is able to read, modify, and write XML data. e.g. you can use the `Get-Content` cmdlet to read the contents of an XML file. or `Set-Content` cmdlet for changes. But you should use at least System.Xml.XmlDocument .NET class to do more advanced XML stuff. – webapp471 Feb 14 '23 at 12:45
  • 1
    Thanks for help @webapp471! It's a bit strange for me, that powershell can't properly deserialize object that was serialized by him. (Normal object) -> powershell -> (XML) -> powershell -> (Normal object with a lot of trash that can't be deserialized without manually parsing System.Xml.XmlDocument) – TommyDiligance Feb 14 '23 at 13:33
  • sure no problem @TommyDiligance would appreciate if you can make my anwser to the solution :) – webapp471 Feb 14 '23 at 15:50