I have a monitoring system that returns JSON data. I am using Powershell to get all of the monitored servers and a subset of their properties. Then I want to export those properties to an XML file that matches an XSD, but I am not sure how to do that.
When I run
$allServers | get-member
I can see all of the properties for the servers in a System.Management.Automation.PSCustomObject. So I run:
$filtered = $allServers | Select-Object Id,HostName,Description,Status
Okay, now I have the properties I want and I can run something like this:
$filtered = ($filtered | ConvertTo-Xml)
and now I have a System.Xml.XmlDocument object. Super. I can use the save method:
$filtered.Save("c:\test\test.xml")
The output looks okay, but does not match the required schema file.
<?xml version="1.0" encoding="utf-8"?>
<Objects>
<Object Type="System.Management.Automation.PSCustomObject">
<Property Name="id" Type="System.Int32">6</Property>
<Property Name="hostname" Type="System.String">server1</Property>
<Property Name="description" Type="System.String">dc</Property>
<Property Name="status" Type="System.Int32">1</Property>
</Object>
</Objects>
My schema looks like this:
<?xml version="1.0" encoding="Windows-1252"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xsd:element name="ServerImport">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Servers">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="Server">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Id" type="xsd:int" />
<xsd:element name="HostName" type="xsd:string" />
<xsd:element name="Description" type="xsd:string" />
<xsd:element name="Status" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xs:schema>
So, how do I make my output match the schema?
Thanks.
<Objects><Object Type="car"><Property Name="make" Type="System.String">Chevy</Property></Object></Objects>.