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.