New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ConvertTo-Xml fails with nested custom objects: omits closing </Objects> tag #8308
Comments
Fails with Windows PowerShell 5.1 so not a regression. Seems specific to nested get-process | select -first 1 | convertto-xml |
Thanks, @SteveL-MSFT. Generally, I only include a Windows PowerShell version in the Environment data section if it isn't a regression (and, conversely, point out if it is). Can you briefly explain the purpose of |
@mklement0 I don't know the history of it, but I can make an educated guess. |
Thanks, @SteveL-MSFT.
So I can suggest improvements to the docs: can you give examples of such other systems? |
I've created a docs issue at MicrosoftDocs/PowerShell-Docs#3310. |
@mklement0 @SteveL-MSFT this does seem specific to pscustomobject. The following works: [psobject] @{ a = [psobject] @{ aa=11; bb=22 } } | ConvertTo-Xml |
@sdwheeler: Yes, but do note that casting to PS> ([psobject] @{ a = [psobject] @{ aa=11; bb=22 } }).GetType().Name
Hashtable # !! [psobject] was effectively ignored. As an aside: I say virtual no-op, because you do get an extra In other words: your command is the same as the following, and therefore operates on a hashtable: @{ a = @{ aa=11; bb=22 } } | ConvertTo-Xml |
@mklement0 the "other systems" was purely hypothetical :) In this day and age, it's probably safe to say it's very uncommon to use XML vs JSON. Other than some demos, I've never had to use |
Looks like the serializer erroneously decreases the serialization PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CustomSerialization.cs Line 735 in 5d03e16
This results in nested properties whenever there's more than one. Removing |
@IISResetMe perhaps you could submit a PR with tests :) |
WriteMemberInfoCollection() incorrectly nests sibling elements at depth==0 The reason seems to be that WriteMemberInfoCollection() calls WriteEndElement() at most once, even if `WriteStartElement()` has been called more than once
See repro below.
As an aside (that will probably become a docs issue): What is the purpose of
ConvertTo-Xml
? The docs suggest that it is the in-memory counterpart toExport-Clixml
, however:Import-Clixml
Steps to reproduce
Expected behavior
Should output an
[xml]
instance that is the serialization of the custom object.Actual behavior
The following error occurs:
If you use
-As String
, you don't get an error, but the XML string output too is missing the closing</Objects>
tag.Removing the 2nd property (
bb
) from the nested custom object makes the problem go away.Using
[hashtable]
instances instead of[pscustomobject]
s makes the problem go away.Export-Clixml
does not exhibit the problem.Environment data
The text was updated successfully, but these errors were encountered: