I am trying to build a new XML file with C# using an existing XSD file. this is the xsd file :
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="KilometerUpload">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="unbounded" name="KilometerRegistration">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="ChassisNumber">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="17" />
<xsd:minLength value="1" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="KilometerStatus">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="7" />
<xsd:minLength value="1" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="TypeOfData">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="3" />
<xsd:minLength value="1" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="ObservationDate">
<xsd:annotation>
<xsd:documentation>Format: yyyyMMdd</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="8" />
<xsd:minLength value="8" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="LegallyResponsible">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10" />
<xsd:minLength value="10" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="EnteredBy">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10" />
<xsd:minLength value="10" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="InternalCode">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10" />
<xsd:minLength value="0" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="DateFirstRegistration">
<xsd:annotation>
<xsd:documentation>Format: yyyyMMdd</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="8" />
<xsd:minLength value="0" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Unifier">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="2" />
<xsd:minLength value="0" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="FeedbackType" type="FeedbackType" use="optional"/>
<xsd:attribute name="FeedbackEmail" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="FeedbackType">
<xsd:annotation>
<xsd:documentation>The feedback type for this file</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="FTP" />
<xsd:enumeration value="EML" />
<xsd:enumeration value="DEF" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
I have done the following things to create the XML file :
- made a class with xsd.exe
- I added the class to my project
wrote the following function :
var data = new KilometerUploadKilometerRegistration { ChassisNumber = huidigefactuur.Wagen.Chassisnummer, KilometerStatus = huidigefactuur.KMStand.ToString(), TypeOfData = "120", }; var serializer = new XmlSerializer(typeof(KilometerUploadKilometerRegistration)); using (var stream = new StreamWriter("C:\\test.xml")) serializer.Serialize(stream, data);
It's working to create the XML file but I need to start at KilometerUpload node and than the KilometerRegistration node how do I do this?
This is the output i get with the code I used above :
<?xml version="1.0" encoding="UTF-8"?>
-<KilometerUploadKilometerRegistration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ChassisNumber>WVWZZZ3CZ7E201402</ChassisNumber>
<KilometerStatus>78000</KilometerStatus>
<TypeOfData>120</TypeOfData>
</KilometerUploadKilometerRegistration>
Thanks !