DataContractSerializer仅部分序列化我的对象

时间:2009-09-01 07:12:34

标签: c# wcf serialization

我使用以下代码将对象序列化为XML,

        StringBuilder sb = new StringBuilder();
        DataContractSerializer dcr = new DataContractSerializer(query.Result.GetType());
        XmlWriterSettings xws = new XmlWriterSettings()
        {
            CloseOutput = true,
            Encoding = Encoding.UTF8
        };
        dcr.WriteObject(XmlWriter.Create(sb, xws), query.Result);
        query.Result.ReportXml = sb.ToString();

但我无法让DataContractSerializer返回完整且有效的XML,对于我尝试序列化的每个对象,我都会返回6143个字符。

以下是返回的XML字符串结尾的示例

<d2p1:anyType i:type="EquipmentDO">
  <DataObjectState>Modified</DataObjectState>
  <DataObjectType>Equipment</DataObjectType>
  <OwningDataManagerType>Configuration</OwningDataManagerType>
  <ConfigurationManagementID i:nil="true" />
  <ConfigurationManagerAction>Nothing</ConfigurationManagerAction>
  <ConfigurationRequestUserID i:nil="true" />
  <Id>10</Id>
  <Active>false</Active>
  <EquipmentModel>11</EquipmentModel>
  <LineClearanceLevelIds>
    <_keys>
      <d2p1:int>1</d2p1:int>
      <d2p1:int>2</d2p1:int>
      <d2p1:int>3</d2p1:int>
      <d2p1:int>4</d2p1:int>
    </_keys>
  </LineClearanceLevelIds>
  <Zone>5</Zone>
</d2p1:anyType>
<d2p1:anyType="" i:type="EquipmentDO">
  <DataObjectState>Modified</DataObjectState>
  <D

是否有其他人经历过将生成的XML限制为多个字符,或者是否有人可以指出我在这里做错了什么?

由于

1 个答案:

答案 0 :(得分:2)

我想它会在XmlWriter缓存;尝试:

using(XmlWriter xw = XmlWriter.Create(sb, xws)) {
    dcr.WriteObject(xw, query.Result);
}