Hi!
I would like to add / remove the fields serialized by
DataContractSerializer at the runtime based on user-options.
The standard scenarion is something like to define:
[DataContract()]
// Apply the KnownTypeAttribute to the class that
// includes a member that returns a Hyperbola.
[KnownType(typeof(CMesurementObjects)]
[KnownType(typeof(Collection<CMesurementObjects>))]
public class MeasurementProfile : IMeasurementProfile
{
/// <summary>
/// Referenced measurement session
/// </summary>
[DataMember]
private Collection<CMesurementObjects> mSessionObjects = new
Collection<CMesurementObjects>;
/// <summary>
/// Referenced measurement session ids
/// </summary>
[DataMember]
private Collection<int> mSessionIDs = new Collection<int>;
....
}
.... somewhere in che code:
//Dictionary write with .NET 3.0 and above
System.IO.FileStream fs = new
System.IO.FileStream(iFileName, System.IO.FileMode.Create);
System.Xml.XmlWriterSettings xws = new
System.Xml.XmlWriterSettings();
xws.Indent = true;
xws.IndentChars = " ";
// This setting helps sets reader/writer intentionally
do not conform XML 1.0 specification and therefore not to generate
Exception for special characters like 0x00
xws.CheckCharacters=false;
System.Xml.XmlWriter writer =
System.Xml.XmlDictionaryWriter.Create(fs, xws);
System.Runtime.Serialization.DataContractSerializer
ser = new
System.Runtime.Serialization.DataContractSerializer(typeof(MeasurementProfile ));
// Use the writer to start a document.
writer.WriteStartDocument(true);
// Use the writer to write the root element.
writer.WriteStartElement("myCompany");
// Use the serializer to write the start,
// content, and end data.
ser.WriteStartObject(writer, mProfile);
ser.WriteObjectContent(writer, mProfile);
ser.WriteEndObject(writer);
// Use the writer to write the end element and
// the end of the document.
writer.WriteEndElement();
writer.WriteEndDocument();
// Close and release the writer resources.
writer.Flush();
fs.Flush();
fs.Close();
.....
(CODE is changed from original!!!!)
.....
But this assumes, all fields marked as [DataMember] would be written
in XML.
What is the way to simple change the serialization on the fly (without
IXMLSerialisation and like other interface surrogates, provide XSD
shemas) and mark, what should and should not to be serialized?
Many thanks in advise!


