我有一个带有重复<row>
标记的输入XML字符串,我试图将其反序列化为一个对象,但除了最后一行之外的所有行都会被忽略。任何帮助,将不胜感激。
例如,在反序列化之后,我得到的对象是:
object.command[0].userTable =
{OCI.OCITable}
colHeading: {string[3]}
colHeadingField: {string[3]}
row: {string[3]}
rowField: {string[3]}
这是错误的,因为此对象中只有一行,但输入XML字符串中应该有4 <row>
,如下所示:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<BroadsoftDocument protocol="OCI" xmlns="C" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sessionId xmlns="">feajiofefeaij</sessionId>
<command echo="" xsi:type="UserGetListInServiceProviderResponse" xmlns="">
<userTable>
<colHeading>User Id</colHeading>
<colHeading>Group Id</colHeading>
<colHeading>Name</colHeading>
<row>
<col>1</col>
<col>A</col>
<col>Smith</col>
</row>
<row>
<col>2</col>
<col>A</col>
<col>John</col>
</row>
<row>
<col>3</col>
<col>B</col>
<col>James</col>
</row>
<row>
<col>4</col>
<col>B</col>
<col>Lisa</col>
</row>
</userTable>
</command>
</BroadsoftDocument>
我进行反序列化的方式是:
MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(responseString));
XmlSerializer ser = new XmlSerializer(typeof(OCIMessage));
OCIMessage response = (OCIMessage)ser.Deserialize(memStream);
由xsd.exe
类为OCITable
类自动生成的C#类是:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "C")]
public partial class OCITable
{
private string[] colHeadingField;
private string[] rowField;
[System.Xml.Serialization.XmlElementAttribute("colHeading", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string[] colHeading
{
get
{
return this.colHeadingField;
}
set
{
this.colHeadingField = value;
}
}
[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("col", typeof(string), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
public string[] row
{
get
{
return this.rowField;
}
set
{
this.rowField = value;
}
}
}
答案 0 :(得分:2)
主要问题是,在您的XML中,<row>
和<col>
元素是一个2d锯齿状数组:有一组包含内部的外部重复<row>
元素,重复<col>
个元素集,每个元素都有一个字符串值。但是在你的OCITable
类中,你已将其建模为1d字符串数组:
public string[] row { get; set; }
这只允许一个外<row>
元素。如果在解析时遇到多个<row>
元素,XmlSerializer
将用更晚的值覆盖之前的值 - 这正是您所看到的。
为了捕获行和列元素的嵌套层次结构,您需要执行以下操作:
public string[][] row { get; set; }
但是,如果这样做,您将遇到C# xml serialization remove jagged array element name中描述的问题,即XmlSerializer
将始终使用额外的外部容器元素序列化2d锯齿状数组,如下所示:
<row>
<col>
<string>a</string>
</col>
<col>
<string>b</string>
</col>
</row>
因此,这也行不通。
相反,您需要使row
成员成为一个简单的中间类数组,其中包含一个字符串数组,如下所示:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "C")]
public partial class OCITable
{
private string[] colHeadingField;
[System.Xml.Serialization.XmlElementAttribute("colHeading", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string[] colHeading
{
get
{
return this.colHeadingField;
}
set
{
this.colHeadingField = value;
}
}
[System.Xml.Serialization.XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public OCITableRow [] row { get; set; }
}
public class OCITableRow
{
[System.Xml.Serialization.XmlElementAttribute("col", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
public string[] col { get; set; }
}
row
和col
数组都标有[XmlElement]
,表示它们应序列化为没有外容器元素的重复元素序列。
使用上面定义的类,您将能够反序列化<userTable>
元素。样本fiddle。