Question about ConvertTo-XML

Topics: User Forum
Jun 16, 2009 at 8:28 PM

Question:
How can I get ConvertTo-XML to use the property name for the container name?
<Property Name="country">USA</Property>
vs.
<country>USA</country>

History:
After some disappointment with the output of Export-Clixml, I found the
ConvertTo-XML Cmdlet included with the great PSCX tools.

This has cleaned up my output greatly.

Initial Export-Clixml output:

<?xml version="1.0" encoding="utf-16"?>
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="title">Empire Burlesque</S>
      <S N="artist">Bob Dylan</S>
      <S N="country">USA</S>
      <S N="company">Columbia</S>
      <S N="price">10.90</S>
      <S N="year">1985</S>
    </MS>
  </Obj>
</Objs>

ConvertTo-XML -NoTypeInformation ouput:

<Objects>
  <Object>
    <Property Name="title">Empire Burlesque</Property>
    <Property Name="artist">Bob Dylan</Property>
    <Property Name="country">USA</Property>
    <Property Name="company">Columbia</Property>
    <Property Name="price">10.90</Property>
    <Property Name="year">1985</Property>
  </Object>
<Objects>

Desired output:

<cdcollection>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
</cdcollection>

The only answer I have found that gets close is here:
http://blogs.msdn.com/powershell/archive/2008/10/18/emit-xml.aspx

However, this code for Emit-XML doesn't use ConvertTo-XML, only exports the final object.

Thanks for any guidance you may be able to give a powershell newbie

Scott

Coordinator
Jun 19, 2009 at 9:35 PM

I think you've got a good answer with the Emit-xml blog post approach.  BTW the ConvertTo-Xml cmdlet is part of PowerShell V2.  There is a similar PSCX cmdlet - Convert-Xml - but it is for applying XSL transforms.