following problem: I want to create an XML document from my database, which should have a specific layout. I created an XSD file, which represents the structure of the XML:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="urn:com.xx.xx" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:com.xx.xx">
<xsd:element name="MT_AnalyseQueueResponse_EXT" type="DT_AnalyseQueue_EXT"/>
<xsd:complexType name="DT_AnalyseQueue_EXT">
<xsd:annotation>
<xsd:documentation xml:lang="EN">Data Type for Analyse Queue Response</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="Res">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Location" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="No" type="xsd:string"/>
<xsd:attribute name="Testpoint" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="Results" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Cylinder" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Detail" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="Component" type="xsd:string"/>
<xsd:attribute name="DeviceID" type="xsd:string"/>
<xsd:attribute name="Value" type="xsd:string"/>
<xsd:attribute name="Info" type="xsd:string"/>
<xsd:attribute name="Result" type="xsd:string"/>
<xsd:attribute name="Unit" type="xsd:string"/>
<xsd:attribute name="Limit" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="Date" type="xsd:string"/>
<xsd:attribute name="BatchNo" type="xsd:string"/>
<xsd:attribute name="Result" type="xsd:string"/>
<xsd:attribute name="Info" type="xsd:string"/>
<xsd:attribute name="BID" type="xsd:string"/>
<xsd:attribute name="Complete" type="xsd:string"/>
<xsd:attribute name="Release" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Using this XSD, Visual Studio shows me an XML created from the XSD, which looks just fine:
<?xml version="1.0" encoding="utf-8"?>
<MT_AnalyseQueueResponse_EXT xmlns="urn:com.xx.xx">
<Res xmlns="">
<Location No="No1" Testpoint="Testpoint1" />
<Results>
<Cylinder Date="Date1" BatchNo="BatchNo1" Result="Result1" Info="Info1" BID="BID1" Complete="Complete1" Release="Release1">
<Detail Component="Component1" DeviceID="DeviceID1" Value="Value1" Info="Info1" Result="Result1" Unit="Unit1" Limit="Limit1" />
</Cylinder>
</Results>
</Res>
</MT_AnalyseQueueResponse_EXT>
Now my problem is, I am not sure how to creat the XML. I used the XSD to create a .VB file, which contains the classes from the XSD. Using those, I wanted to fill the different elements and attributes and use a serializer and XML writer to create the XML file, but it is not working, I just get this output:
<?xml version="1.0" encoding="iso-8859-1"?>
<DT_AnalyseQueue_EXTResLocation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" No="2000" Testpoint="MED12" />
I guess the problem is somewhere hidden in the part where I use the different classes and try to add them together. Here is my code:
Dim MyCylResultsLoc As DT_AnalyseQueue_EXTResLocation = New DT_AnalyseQueue_EXTResLocation() _
With {.No = sLocNo, .Testpoint = drRes("TP")}
Dim MyCylResultsResCylDet As DT_AnalyseQueue_EXTResResultsCylinderDetail = New DT_AnalyseQueue_EXTResResultsCylinderDetail() _
With {.Component = drRes("AA"),
.DeviceID = drRes("BB"),
.Result = drRes("CC"),
.Value = drRes("VV"),
.Unit = drRes("UU")}
' 'write XML export file with ISO-8859-1 encoding and \r\n line feed
Dim MyCylResultsResCyl As DT_AnalyseQueue_EXTResResultsCylinder = New DT_AnalyseQueue_EXTResResultsCylinder() _
With {.BatchNo = drRes("Ba"),
.BID = drRes("B"),
.Complete = drRes("CC"),
.Date = sPruevDatuB,
.Result = drRes("RR"),
.Detail = ?????} ' here I would use the class name of the detail branch but that is not working
'28591
Dim MyXMLWriterSettings As Xml.XmlWriterSettings = New Xml.XmlWriterSettings _
With {.Encoding = System.Text.Encoding.GetEncoding(28591), .Indent = True, .IndentChars = vbTab, .NewLineChars = vbCrLf}
Dim MyXMLWriter As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(sExportPath & sExportFileName & ".XML", MyXMLWriterSettings)
Dim MyXMLSerializerLoc As New XmlSerializer(GetType(DT_AnalyseQueue_EXTResLocation))
MyXMLSerializerLoc.Serialize(MyXMLWriter, MyCylResultsLoc)
Dim MyXMLSerializerCyl As New XmlSerializer(GetType(DT_AnalyseQueue_EXTResResultsCylinder))
MyXMLSerializerCyl.Serialize(MyXMLWriter, MyCylResultsResCyl)
Dim MyXMLSerializercylDet As New XmlSerializer(GetType(DT_AnalyseQueue_EXTResResultsCylinderDetail))
MyXMLSerializercylDet.Serialize(MyXMLWriter, MyCylResultsResCylDet)
MyXMLWriter.Close()
Anyone that can push me in the right direction? EDIT: SOME MORE INFORMATION
Ok, while I got a working example, it is not excatly what I would like to have. First, within one XML I should have several sub elements of the same name like in the example XML produced by my XSD file:
<?xml version="1.0" encoding="utf-8"?>
<MT_AnalyseQueueResponse_EXT xmlns="urn:com.xxx.xxx">
<Res xmlns="">
<Location No="No1" Testpoint="Testpoint1" />
<Results>
<Cylinder Date="Date1" BatchNo="BatchNo1" Result="Result1" Info="Info1" BID="BID1" Complete="Complete1" Release="Release1">
<Detail Component="Component1" DeviceID="DeviceID1" Value="Value1" Info="Info1" Result="Result1" Unit="Unit1" Limit="Limit1" />
</Cylinder>
<Cylinder Date="Date2" BatchNo="BatchNo2" Result="Result2" Info="Info2" BID="BID2" Complete="Complete2" Release="Release2">
<Detail Component="Component4" DeviceID="DeviceID4" Value="Value4" Info="Info4" Result="Result4" Unit="Unit4" Limit="Limit4" />
<Detail Component="Component5" DeviceID="DeviceID5" Value="Value5" Info="Info5" Result="Result5" Unit="Unit5" Limit="Limit5" />
</Cylinder>
<Cylinder Date="Date3" BatchNo="BatchNo3" Result="Result3" Info="Info3" BID="BID3" Complete="Complete3" Release="Release3">
<Detail Component="Component7" DeviceID="DeviceID7" Value="Value7" Info="Info7" Result="Result7" Unit="Unit7" Limit="Limit7" />
<Detail Component="Component8" DeviceID="DeviceID8" Value="Value8" Info="Info8" Result="Result8" Unit="Unit8" Limit="Limit8" />
</Cylinder>
</Results>
<Results>
<Cylinder Date="Date1" BatchNo="BatchNo1" Result="Result1" Info="Info1" BID="BID1" Complete="Complete1" Release="Release1">
<Detail Component="Component1" DeviceID="DeviceID1" Value="Value1" Info="Info1" Result="Result1" Unit="Unit1" Limit="Limit1" />
</Cylinder>
</Results>
</Res>
</MT_AnalyseQueueResponse_EXT>
The number of substructures may be varying. This is why I thought I could use my *.vb file which is compiled using VS from my XSD:
Option Strict Off
Option Explicit On
Imports System.Xml.Serialization
'
'This source code was auto-generated by xsd, Version=4.0.30319.1.
'
'<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:com.xx.xx"), _
System.Xml.Serialization.XmlRootAttribute("MT_AnalyseQueueResponse_EXT", [Namespace]:="urn:com.xxx.xx", IsNullable:=false)> _
Partial Public Class DT_AnalyseQueue_EXT
Private resField As DT_AnalyseQueue_EXTRes
'<remarks/>
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property Res() As DT_AnalyseQueue_EXTRes
Get
Return Me.resField
End Get
Set
Me.resField = value
End Set
End Property
End Class
'<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="urn:com.xxx.xx")> _
Partial Public Class DT_AnalyseQueue_EXTRes
Private locationField() As DT_AnalyseQueue_EXTResLocation
Private resultsField()() As DT_AnalyseQueue_EXTResResultsCylinder
Sub New()
' TODO: Complete member initialization
End Sub
'<remarks/>
<System.Xml.Serialization.XmlElementAttribute("Location", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property Location() As DT_AnalyseQueue_EXTResLocation()
Get
Return Me.locationField
End Get
Set
Me.locationField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
System.Xml.Serialization.XmlArrayItemAttribute("Cylinder", GetType(DT_AnalyseQueue_EXTResResultsCylinder), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=false)> _
Public Property Results() As DT_AnalyseQueue_EXTResResultsCylinder()()
Get
Return Me.resultsField
End Get
Set
Me.resultsField = value
End Set
End Property
End Class
'<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="urn:com.xx.xx")> _
Partial Public Class DT_AnalyseQueue_EXTResLocation
Private noField As String
Private testpointField As String
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property No() As String
Get
Return Me.noField
End Get
Set
Me.noField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Testpoint() As String
Get
Return Me.testpointField
End Get
Set
Me.testpointField = value
End Set
End Property
End Class
'<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="urn:com.xxx.xx")> _
Partial Public Class DT_AnalyseQueue_EXTResResultsCylinder
Private detailField() As DT_AnalyseQueue_EXTResResultsCylinderDetail
Private dateField As String
Private batchNoField As String
Private resultField As String
Private infoField As String
Private bIDField As String
Private completeField As String
Private releaseField As String
'<remarks/>
<System.Xml.Serialization.XmlElementAttribute("Detail", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Property Detail() As DT_AnalyseQueue_EXTResResultsCylinderDetail()
Get
Return Me.detailField
End Get
Set
Me.detailField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property [Date]() As String
Get
Return Me.dateField
End Get
Set
Me.dateField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property BatchNo() As String
Get
Return Me.batchNoField
End Get
Set
Me.batchNoField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Result() As String
Get
Return Me.resultField
End Get
Set
Me.resultField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Info() As String
Get
Return Me.infoField
End Get
Set
Me.infoField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property BID() As String
Get
Return Me.bIDField
End Get
Set
Me.bIDField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Complete() As String
Get
Return Me.completeField
End Get
Set
Me.completeField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Release() As String
Get
Return Me.releaseField
End Get
Set
Me.releaseField = value
End Set
End Property
End Class
'<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="urn:com.xxx.xxx")> _
Partial Public Class DT_AnalyseQueue_EXTResResultsCylinderDetail
Private componentField As String
Private deviceIDField As String
Private valueField As String
Private infoField As String
Private resultField As String
Private unitField As String
Private limitField As String
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Component() As String
Get
Return Me.componentField
End Get
Set
Me.componentField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property DeviceID() As String
Get
Return Me.deviceIDField
End Get
Set
Me.deviceIDField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Value() As String
Get
Return Me.valueField
End Get
Set
Me.valueField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Info() As String
Get
Return Me.infoField
End Get
Set
Me.infoField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Result() As String
Get
Return Me.resultField
End Get
Set
Me.resultField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Unit() As String
Get
Return Me.unitField
End Get
Set
Me.unitField = value
End Set
End Property
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Limit() As String
Get
Return Me.limitField
End Get
Set
Me.limitField = value
End Set
End Property
End Class
Try this