【发布时间】:2012-02-04 06:01:23
【问题描述】:
我有以下课程:
public class MainRequest
{
private Request _dataField;
[XmlElementAttribute("Parameters")]
public Request Parameters
{
get
{
return _dataField;
}
set
{
_dataField = value;
}
}
}
public class Request
{
private RequestSize _requestSize;
private Field[][] _field;
[XmlElementAttribute(IsNullable = true)]
public RequestSize RequestSize
{
get
{
return _requestSize;
}
set
{
_requestSize = value;
}
}
[XmlArrayItem("BatchEntry")]
[XmlArrayItemAttribute("ParameterEntry", IsNullable = false, NestingLevel = 1)]
public Field[][] Batch
{
get
{
return _field;
}
set
{
_field = value;
}
}
}
public class RequestSize
{
private string _count;
private string _value;
[XmlAttributeAttribute]
public string Count
{
get
{
return _count;
}
set
{
_count = value;
}
}
[XmlTextAttribute]
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
}
public class Field
{
private string _fieldName;
private string _fieldValue;
[XmlAttributeAttribute(AttributeName = "name")]
public string Name
{
get
{
return _fieldName;
}
set
{
_fieldName = value;
}
}
[XmlTextAttribute]
public string Value
{
get
{
return _fieldValue;
}
set
{
_fieldValue = value;
}
}
}
当它们被序列化时,我得到:
<Parameters>
<RequestSize Count="1">2</RequestSize>
<Batch>
<BatchEntry>
<ParameterEntry name="AAA">111</ParameterEntry>
<ParameterEntry name="BBB">222</ParameterEntry>
<ParameterEntry name="CCC">333</ParameterEntry>
</BatchEntry>
</Batch>
</Parameters>
我正在尝试摆脱 Batch 元素。我希望 xml 看起来像:
<Parameters>
<RequestSize Count="1">2</RequestSize>
<BatchEntry>
<ParameterEntry name="AAA">111</ParameterEntry>
<ParameterEntry name="BBB">222</ParameterEntry>
<ParameterEntry name="CCC">333</ParameterEntry>
</BatchEntry>
</Parameters>
我尝试在 Field[][] 上使用 XmlElement 属性,但这样做时出现错误:
[XmlElement("Batch")]
public Field[][] Batch
{
get
{
return _field;
}
set
{
_field = value;
}
}
Error 97 Cannot convert type 'Field[][]' to 'Field[]'
有没有一种方法可以在没有顶级元素名称的情况下序列化数组元素?
【问题讨论】:
-
我刚刚编辑了我的答案,试试看。我认为它应该有效。
标签: c# xml serialization