Vandoorn Kevin
Vandoorn Kevin

Reputation: 205

Generate XML file in C# with XSD file

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 :

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 !

Upvotes: 2

Views: 17902

Answers (4)

Vandoorn Kevin
Vandoorn Kevin

Reputation: 205

I have found the answer :

  • I did not need to change my class generated by the xsd.exe

This is the code I ended with, it works perfectly now :

                foreach (Factuur huidigeFactuur2 in e.SelectedObjects)
            {
                XmlSerializer serializer2 = new XmlSerializer(typeof(KilometerUpload));
                TextWriter writer = new StreamWriter(@"C:\test2.xml");

                string chassisnummer = huidigeFactuur2.Wagen.Chassisnummer;
                string kilometerstatus = huidigeFactuur2.KMStand.ToString();

                KilometerUpload item = new KilometerUpload
                {
                    KilometerRegistration = new KilometerUploadKilometerRegistration[] { new KilometerUploadKilometerRegistration{ ChassisNumber = chassisnummer , TypeOfData = "120", KilometerStatus = kilometerstatus} },
                };

                serializer2.Serialize(writer, item);

Upvotes: 4

hschne
hschne

Reputation: 704

Building an xml from xsd in code will be tiresome. In the past I have used the XMLSampleGenerator by Priya Lakshminarayanan. The sourcecode to this particular project is found here.

I needed to do pretty much what you wanted, but the sample generation of visual studio was not sufficient. The XmlSampleGenerator I provided a link to is similar to the VS sample generation, but obviously you have the source code, and as such you can debug and adjust it to your needs (in my case I needed proper sample regex expression generation).

A word of warning: Generating xmls from a xsd is no easy task, so dont expect it to be. Good luck.

Edit: This code will generate an Xml that also contains a KilometerUpload Node.

namespace XmlGen {
public class Program {
    private static void Main( string[] args ) {

        XmlSerializer serializer = new XmlSerializer(typeof(XmlItem));
        TextWriter writer = new StreamWriter(@"C:\Users\hasch\Downloads\test.xml");
        XmlItem item = new XmlItem();
        serializer.Serialize(writer,item);
    }
}



public class XmlItem {

    public KilometerUpload KilometerUpload;

    public KilometerRegistration KilometerRegistration;
    
    public XmlItem() {
        KilometerUpload = new KilometerUpload();
        KilometerRegistration = new KilometerRegistration();
    }
}

public class KilometerUpload {


}

public class KilometerRegistration {

    public string ChassisNumber { get; set; }

    public string KilometerStatus { get; set; }

    public string TypeOfData { get; set; }

    public KilometerRegistration() {
        ChassisNumber = "WVWZZZ3CZ7E201402";

        KilometerStatus = "78000";

        TypeOfData = "120";

    }}}

And this is the generated Xml:

<?xml version="1.0" encoding="utf-8"?> 
  <XmlItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/2001/XMLSchema" >                    
<KilometerUpload />
 <KilometerRegistration>
  <ChassisNumber>WVWZZZ3CZ7E201402</ChassisNumber>
   <KilometerStatus>78000</KilometerStatus>
  <TypeOfData>120</TypeOfData>
</KilometerRegistration>

Do keep in mind that this is in fact NOT generating xml from an xsd! If your xsd changes you will have to make major changes to your classes and such.

Upvotes: 0

r.sumesh
r.sumesh

Reputation: 333

your serialiser is at KilometerUpdateKilometerRegistration have you tried to set that at

var serializer = new XmlSerializer(typeof(kilometerUpload));

Upvotes: 1

user2953680
user2953680

Reputation: 594

to write an xml file you can use XmlWriter class.

here is a tutorial

http://www.dotnetperls.com/xmlwriter

Upvotes: 0

Related Questions