Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Indigo

Vista - How to programmatically add/remove fields to DataContract?

 
 
Old 02-17-2008   #1 (permalink)
infalanda


 
 

How to programmatically add/remove fields to DataContract?

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!

My System SpecsSystem Spec
 

Thread Tools



Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46