none
How to get properly typed data from xml in Powershell RRS feed

  • Question

  • When i read xml data as objects (dot notation),  the properties of the objects are all [string] while the proper data types are in the <schema>  part of the xml document.

    sample file:

    <doc>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="doc">
                <xs:complexType>
                    <xs:choice maxOccurs="unbounded">
                        <xs:element name="item">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="astring" type="xs:string" minOccurs="0"/>
                                    <xs:element name="along" type="xs:long" minOccurs="0"/>
                                    <xs:element name="adate" type="xs:dateTime" minOccurs="0"/>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:choice>
                </xs:complexType>
            </xs:element>
        </xs:schema>
        <item>
            <astring>qwerty</astring>
            <along>12345</along>
            <adate>2017-06-09T13:18:30</adate>
        </item>
    </doc>

    When i do:

    PS>[xml]$xml=get-content("d:\temp\sample.xml")
    PS>$xml.doc.item|get-member

    it shows:

    adate                Property              string adate {get;set;}
    along                Property              string along {get;set;}
    astring              Property              string astring {get;set;}

    Is this normal or am i doing something wrong?
    Maybe the question is too simple but i couldn't find an answer in the forum archive.

    Thx,

    Roel

    • Moved by Bill_Stewart Tuesday, November 7, 2017 9:19 PM Off-topic
    Wednesday, September 6, 2017 9:38 AM

All replies

  • Hi Roel,

    this is working as designed. Xml is understood as a pure string document in PowerShell, because that is how the XML Document type in C# handles it. The only XML it natively converts to types are ...

    • Those imported by Import-Clixml (which usually only works on stuff exported by Export-Clixml)
    • Object types for which conversion was explicitly defined

    You'll probably need to build your own conversion logic or find a solution someone else found and built for it - it's not included out-of-the-box.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, September 6, 2017 11:28 AM