XSD not generating C# class as I expect
I have the following xml file:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header xmlns:hdr="http://xml.example.com/ns/appmw/soap/1.0/header">
<hdr:FinalMessage>true</hdr:FinalMessage>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<MyResponse>
<Result seqNo="0">
<MetaData>
<DataSchema>
<ColumnSchema type="char" ref="item1" name="Item1"/>
<ColumnSchema type="char" ref="item2" name="Item2"/>
<ColumnSchema type="char" ref="item3" name="Item3"/>
</DataSchema>
</MetaData>
<Data>
<Item>
<Column ref="item1">Foo</Column>
<Column ref="item2">Bar</Column>
<Column ref="item3">Baz</Column>
</Item>
<Item>
<Column ref="item1">Foo</Column>
<Column ref="item2">Bar</Column>
<Column ref="item3">Baz</Column>
</Item>
</Data>
</Result>
</MyResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Using xsd, I generated xsd files and then a C# class from those files. It works and the deserialization is fine, but the Data part class that was generated is not what I expected.
Here is the relevant xsd section:
<xs:element name="Data" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Column" nillable="true" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent msdata:ColumnName="Column_Text" msdata:Ordinal="1">
<xs:extension base="xs:string">
<xs:attribute name="ref" form="unqualified" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
There will be a single Data section that can have a variable number of Items. Each Item can have a variable number of Columns.
Here is what was generated for the class:
private MyResponseResultItemColumn[][] dataField;
[System.Xml.Serialization.XmlArrayItemAttribute("Item", IsNullable=false)]
[System.Xml.Serialization.XmlArrayItemAttribute("Column", NestingLevel=1)]
public MyResponseResultItemColumn[][] Data {
get {
return this.dataField;
}
set {
this.dataField = value;
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[Sys开发者_运维知识库tem.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class MyResponseResultItemColumn {
private string refField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string @ref {
get {
return this.refField;
}
set {
this.refField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
As you can see, xsd made Data an array of arrays (array of ItemColumn arrays). What I was expecting and want to see is Data as simple array of an Item class, where the Item class contains an array of ItemColumns as a property.
Is there a way I can achieve this? Instead of MyResponse.Result.Data[0][0] returning an array of ItemColumn, I want to be able to type MyResponse.Result.Data[0].ItemColumns.
You can write your own serializer by implementing the IXmlSerializable interface. It's a bit troublesome, but you have complete control over the shape of the object. This is a pretty good article on how to implement it, which I found in a thread on stackoverflow
精彩评论