我有一个输入XML字符串,其中包含重复的<row>
标记,我试图将其反序列化为对象,但除最后一行外的所有行都被忽略。任何帮助都会很感激。
作为一个例子,反序列化之后,我得到的对象是:
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;
}
}
}
主要问题是,在您的XML中,<row>
和<col>
元素是一个2d锯齿数组:有一个外部的、重复的<row>
元素集,包含一个内部的、重复的<col>
元素集,每个元素都有一个字符串值。但在OCITable
课程中你已经将其建模为字符串的1d数组:
public string[] row { get; set; }
这只允许一个外部<row>
元素。如果在解析时遇到多个<row>
元素,XmlSerializer
将用较晚的值覆盖较早的值——这正是您所看到的。
为了捕获行和列元素的嵌套层次结构,您需要做如下操作:
public string[][] row { get; set; }
但是,如果您这样做,您将遇到c# xml序列化中描述的问题,即XmlSerializer
将始终序列化带有额外外部容器元素的2d锯齿数组,如下所示:
<row>
<col>
<string>a</string>
</col>
<col>
<string>b</string>
</col>
</row>
因此这也不能工作。
相反,您需要将row
成员设置为一个简单的中间类数组,其中包含一个1d字符串数组,如下所示:
[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>
元素。示例小提琴。