views:

156

answers:

1

I am using the following code to serialize an object to 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();

But I cannot get the DataContractSerializer to return complete and valid XML, for every object I try to serialize, I get 6143 characters returned.

Here is an example of the end of the XML string that is returned,

<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

Has anyone else experienced the capping of the resulting XML to a number of characters or can anyone point out what I am doing wrong here?

Thanks

+1  A: 

I imagine it is buffered in the XmlWriter; try:

using(XmlWriter xw = XmlWriter.Create(sb, xws)) {
    dcr.WriteObject(xw, query.Result);
}
Marc Gravell
Yep, that did it, thanks a lot Marc.
mattdlong