Generate XML file in C# with XSD file
16,114
Solution 1
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);
Solution 2
your serialiser is at KilometerUpdateKilometerRegistration
have you tried to set that at
var serializer = new XmlSerializer(typeof(kilometerUpload));
Author by
Vandoorn Kevin
Updated on June 04, 2022Comments
-
Vandoorn Kevin almost 2 years
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 !
-
Vandoorn Kevin over 10 yearsIsn't there a shorter way using the XML serializatin? I allready generated an xml file with this short piece of code but I need different nodes
-
Vandoorn Kevin over 10 yearsI updated my question with the output my code now generates. As you can see my starting node is the KilometerUpdateKilometerRegistration this is not what I want my first node needs to be kilometerUpload and the second node is KilometerRegistration
-
Vandoorn Kevin over 10 yearsI haven't tried this yet. Can the serializer be different from the var data?
-
Vandoorn Kevin over 10 yearsI have tried to set the serializer at Kilometerupload but when i run it I get this error : "There was an error generating the XML document"
-
Vandoorn Kevin over 10 yearsI think I am really close , I only need 1 more node to the XML file and it's completed. But I just dont know how to split the KilometerUpdateKilometerRegistration node into 2.
-
hschne over 10 yearsI am not sure I understand. Do you want the KilometerUpdateKilometerRegistration do be present two times? Like with different data? Or do you mean a subnode is missing (e.g. ObservationDate)? Are you using the XmlSerializer solution you posted above?
-
Vandoorn Kevin over 10 yearsYes I am using the XMLSerializer I stated above. The problem is now i have 1 node : KilometerUpdateKilometerRegistration . I need 2 nodes first KilometerUpdate than Kilometerregistration like in my XSD file.
-
hschne over 10 yearsAdded sample code. Is this helpful? Btw, its "then" not "than", but lots german speaking people make that mistake ;) Mahlzeit!
-
Vandoorn Kevin over 10 yearsThanks for the example ! I know if my XSD changes I will have to change the class, but this XSD file hasn't changed in years. I noticed that you initialise the properties inside a class but I'm trying to generate the XML file through a method that is not in that class. The data that fills the XML file comes from a different class that's why i use a different method
-
hschne over 10 yearsGlad to help. The solution i provided was a draft, so feel free to change it to fit your needs. I'd appreciate it if you would accept my answer (if it did help you solve your problem!)
-
Vandoorn Kevin over 10 yearsTo use this solution I have to change my class? This class was generated by the xsd.exe tool that VS delivers. Can't I do this without changing the class?
-
hschne over 10 yearsYou dont need to change your class. The only problem is, that Xsd.exe does not generate a class for the simple type KilometerUpload. You only need to add this as a class if you want it to be added to your xml. Since you mentioned the xsd doesnt actually change that shouldnt be a problem. To avoid a comment discussion i suggest you play around with xml serializer to get a feel for what gets mapped and how. I'm afraid i cannot help you any further. Good luck!
-
Vandoorn Kevin over 10 yearsOke thanks for the help !! I understand it way better now ! I have accepted your answer, thanks again !
-
Vandoorn Kevin over 10 yearsI noticed something about your class. the class begins with the node XMLItem, mine begins with KilometerUpload so I don't need to add the class it's allready there but as the XMLItem you implemented so this is not quite the solution I was looking for. Could you help me a bit more?