Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate XML file in C# with XSD file
    text
    copied!<p>I am trying to build a new XML file with C# using an existing XSD file. this is the xsd file : </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;xsd:element name="KilometerUpload"&gt; &lt;xsd:complexType&gt; &lt;xsd:sequence&gt; &lt;xsd:element minOccurs="1" maxOccurs="unbounded" name="KilometerRegistration"&gt; &lt;xsd:complexType&gt; &lt;xsd:sequence&gt; &lt;xsd:element name="ChassisNumber"&gt; &lt;xsd:simpleType&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:maxLength value="17" /&gt; &lt;xsd:minLength value="1" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:element&gt; &lt;xsd:element name="KilometerStatus"&gt; &lt;xsd:simpleType&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:maxLength value="7" /&gt; &lt;xsd:minLength value="1" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:element&gt; &lt;xsd:element name="TypeOfData"&gt; &lt;xsd:simpleType&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:maxLength value="3" /&gt; &lt;xsd:minLength value="1" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:element&gt; &lt;xsd:element name="ObservationDate"&gt; &lt;xsd:annotation&gt; &lt;xsd:documentation&gt;Format: yyyyMMdd&lt;/xsd:documentation&gt; &lt;/xsd:annotation&gt; &lt;xsd:simpleType&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:maxLength value="8" /&gt; &lt;xsd:minLength value="8" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:element&gt; &lt;xsd:element name="LegallyResponsible"&gt; &lt;xsd:simpleType&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:maxLength value="10" /&gt; &lt;xsd:minLength value="10" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:element&gt; &lt;xsd:element name="EnteredBy"&gt; &lt;xsd:simpleType&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:maxLength value="10" /&gt; &lt;xsd:minLength value="10" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:element&gt; &lt;xsd:element name="InternalCode"&gt; &lt;xsd:simpleType&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:maxLength value="10" /&gt; &lt;xsd:minLength value="0" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:element&gt; &lt;xsd:element name="DateFirstRegistration"&gt; &lt;xsd:annotation&gt; &lt;xsd:documentation&gt;Format: yyyyMMdd&lt;/xsd:documentation&gt; &lt;/xsd:annotation&gt; &lt;xsd:simpleType&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:maxLength value="8" /&gt; &lt;xsd:minLength value="0" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:element&gt; &lt;xsd:element name="Unifier"&gt; &lt;xsd:simpleType&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:maxLength value="2" /&gt; &lt;xsd:minLength value="0" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:element&gt; &lt;/xsd:sequence&gt; &lt;/xsd:complexType&gt; &lt;/xsd:element&gt; &lt;/xsd:sequence&gt; &lt;xsd:attribute name="FeedbackType" type="FeedbackType" use="optional"/&gt; &lt;xsd:attribute name="FeedbackEmail" type="xsd:string" use="optional"/&gt; &lt;/xsd:complexType&gt; &lt;/xsd:element&gt; &lt;xsd:simpleType name="FeedbackType"&gt; &lt;xsd:annotation&gt; &lt;xsd:documentation&gt;The feedback type for this file&lt;/xsd:documentation&gt; &lt;/xsd:annotation&gt; &lt;xsd:restriction base="xsd:string"&gt; &lt;xsd:enumeration value="FTP" /&gt; &lt;xsd:enumeration value="EML" /&gt; &lt;xsd:enumeration value="DEF" /&gt; &lt;/xsd:restriction&gt; &lt;/xsd:simpleType&gt; &lt;/xsd:schema&gt; </code></pre> <p>I have done the following things to create the XML file :</p> <ul> <li>made a class with xsd.exe</li> <li>I added the class to my project</li> <li><p>wrote the following function : </p> <pre><code> 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); </code></pre></li> </ul> <p>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? </p> <p>This is the output i get with the code I used above :</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; -&lt;KilometerUploadKilometerRegistration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;ChassisNumber&gt;WVWZZZ3CZ7E201402&lt;/ChassisNumber&gt; &lt;KilometerStatus&gt;78000&lt;/KilometerStatus&gt; &lt;TypeOfData&gt;120&lt;/TypeOfData&gt; &lt;/KilometerUploadKilometerRegistration&gt; </code></pre> <p>Thanks !</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload