Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Indigo

How to programmatically add/remove fields to DataContract?

Update your Vista Drivers Update Your Drivers Now!!
 
 
Thread Tools Display Modes
Old 02-17-2008   #1 (permalink)
infalanda
Guest


 

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
Display Modes




Update your Vista Drivers Update Your Vista Drivers Now!!

Vistax64.com 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 2005-2008