Skip to content

ConvertTo-Xml fails with nested custom objects: omits closing </Objects> tag #8308

@mklement0

Description

@mklement0

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 to Export-Clixml, however:

  • It produces different XML representations,
  • which, if saved to a file, cannot be re-imported with Import-Clixml

Steps to reproduce

[pscustomobject] @{ a = [pscustomobject] @{ aa=11; bb=22 } } | ConvertTo-Xml

Expected behavior

Should output an [xml] instance that is the serialization of the custom object.

Actual behavior

The following error occurs:

ConvertTo-Xml : Unexpected end of file has occurred. The following elements are not closed: Objects.
  • 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

PowerShell Core 6.2.0-preview.1 on macOS 10.14.1
PowerShell Core 6.2.0-preview.1 on Ubuntu 16.04.5 LTS
PowerShell Core 6.2.0-preview.1 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.345)
Windows PowerShell v5.1.17134.228 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.345)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions