:: Home

  login:         
  passwords:  

WPF FAQs

WPF General Questions
Concepts Element Tree
Concepts Dependency Property
Concepts Attached Property
Concepts Routed Events
Concepts Resources.
Concepts Animation
Concepts Freezable Object
Concepts Input and Commands.
Concepts Layouts
XAML Inline Styles and Templates
XAML XML Namespaces
XAML Code Behind
XAML Custom Class
XAML Type Converters
Content Model ItemControl
Content Model HeaderedItemsControl
Content Model HeaderedContentControl
Content Model Content Control
Content Model Decorator Content Model
Content Model Panel Content Model
Documents Serialization and Storage
Documents Annotations
Documents Flow Content
Documents Printing
Graphics and Multimedia Rendering Graphics
Graphics and Multimedia Animation and Timing
Graphics and Multimedia 2D Graphics
Graphics and Multimedia 3D Graphics
Graphics and Multimedia Visual Layer
Control Customization Adorners
Control Customization Stylable controls
Control Customization ControlTemplates
Data Data Binding
Data DragandDrop
Data Serialization
Globalization and Localization Attributes
Globalization and Localization Comments
Globalization and Localization Globalization Struc
Application and Deployment ClickOnce
Application and Deployment Frame
Application and Deployment Page
Application and Deployment Navigation
Application and Deployment Setup
Application and Deployment Window
Interoperability Message Loops Between
Interoperability Win32 in WPF
Interoperability WPF in Win32
Interoperability Windows Forms and WPF
Security Trusted Security
Security Partial Trust Security
Tools Microsoft Expression Blend
Tools ZAM3D
Tools XAMLPAD
Controls ToolTip
Controls TextBlock
Controls TabControl
Controls ProgressBar
Controls PrintDialog
Controls Popup
Controls TextBox
Controls Canvas
Controls ComboBox
Controls ListBox
Controls StatusBar
Controls ToolBar
Controls Context Menu
Controls Expander
Controls DocumentViewer
Controls FlowDocumentReader
Controls GroupBox
Controls GridSplitter
Controls Image
Controls Menu
Controls ScrollBar
Controls Slider
Controls RichTextBox
Controls Border
Controls Buttons

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

Winforms Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

Documents Serialization and Storage Questions & FAQs

Questions and answers for Documents Serialization and Storage.

22. Documents Serialization and Storage

    22.1 How can I store a given document in a specific format using CreateSerializerWriter methods ?
    22.2 How can I use MemoryStream to create a deep clone of a serializable object ?
    22.3 How can I do versioning using Custom Serialization ?

22.1 How can I store a given document in a specific format using CreateSerializerWriter methods ?

You can store a given document format using CreateSerializerWriter method as follows.


[C#]

// Create a SerializerProvider for accessing plug-in serializers.
SerializerProvider serializerProvider = new SerializerProvider();

// Locate the serializer that matches the fileName extension.
SerializerDescriptor selectedPlugIn = null;
foreach ( SerializerDescriptor serializerDescriptor in
serializerProvider.InstalledSerializers )
{
if ( serializerDescriptor.IsLoadable &&
fileName.EndsWith(serializerDescriptor.DefaultFileExtension) )
{ // The plug-in serializer and fileName extensions match.
selectedPlugIn = serializerDescriptor;
break; // foreach
}
}

// If a match for a plug-in serializer was found,
// use it to output and store the document.
if (selectedPlugIn != null)
{
Stream package = File.Create(fileName);
SerializerWriter serializerWriter =
serializerProvider.CreateSerializerWriter(selectedPlugIn,
package);
IDocumentPaginatorSource idoc =
flowDocument as IDocumentPaginatorSource;
serializerWriter.Write(idoc.DocumentPaginator, null);
package.Close();
return true;
}



22.2 How can I use MemoryStream to create a deep clone of a serializable object ?

This can be done as follows.


[C#]

public static class SerializationUtil
{
static public T Clone(T source)
{
Debug.Assert(typeof(T).IsSerializable);


IGenericFormatter formatter = new GenericBinaryFormatter();
Stream stream = new MemoryStream();
formatter.Serialize(stream,source);
stream.Seek(0,SeekOrigin.Begin);

T clone = formatter.Deserialize(stream);
stream.Close();
return clone;
}
//Rest of SerializationUtil
}



22.3 How can I do versioning using Custom Serialization ?

Versioning can be done using serialization as follows.


[C#]
//Version 2.0.0.0
[Serializable]
public class MyClass : ISerializable
{
public int Number;
public int NewField;

public void GetObjectData(
SerializationInfo info,StreamingContext context)
{
info.AddValue("Number",Number);
info.AddValue("NewField",NewField);
}

protected MyClass(SerializationInfo info,StreamingContext context)
{
Number = info.GetInt32("Number");
Version storedVersion = SerializationUtil.GetVersion(info);
if(storedVersion.ToString() == "2.0.0.0")
{
NewField = info.GetInt32("NewField");
}
else
{
NewField = 123;//Some default value
}
}

public MyClass()
{}
}

public static class SerializationUtil
{
static public Version GetVersion(SerializationInfo info)
{
string assemblyName = info.AssemblyName;
/* AssemblyName is in the form of "MyAssembly, Version=1.2.3.4,
Culture=neutral,PublicKeyToken=null" */
char[] separators = {',','='};
string[] nameParts = assemblyName.Split(separators);
return new Version(nameParts[2]);
}
//Rest of SerializationUtil
}

bottom user control
::  Home :: Services ::  Prices ::  Request Quote
Copyright 2007, Megasolutions Ltd