Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
(Windows 8 Apps, C#)
Hi,
I have a number of Images(.jpg) and Audio-files(.mp3) in each object. I would like to store these objects in separate files and retrieve the content later. I searched for Object Serialization, but could not find a right example for Windows 8 apps.
So, what are the ways of saving the objects to files in Windows 8 apps(C#).
If it is Object Serialization, please give a sample piece of code or a link to it.
Posted 8-May-13 5:46am
Edited 8-May-13 6:20am
v4
Comments
Sergey Alexandrovich Kryukov at 8-May-13 11:07am
   
Why do you mix up serialization and Metro together. Ever heard of separation of concerns?
—SA
Sergey Alexandrovich Kryukov at 8-May-13 11:12am
   
Platform? Language? Anything?... You need to tag relevant information.
—SA
Abhinav Varma at 8-May-13 12:10pm
   
ya..sorry for that..I am using C# on .NET
Sergey Alexandrovich Kryukov at 8-May-13 12:54pm
   
OK, then my answer will fully solve your problem, please see.
—SA
Abhinav Varma at 19-May-13 6:08am
   
would you help me solving the problem in my last post sir..
Sergey Alexandrovich Kryukov at 19-May-13 13:14pm
   
Which is?
—SA
Abhinav Varma at 19-May-13 13:17pm
   
the one in the bottom with the name "Solution 4"....
Sergey Alexandrovich Kryukov at 19-May-13 13:49pm
   
Don't try to serialize "ImageSource". What is that, the whole image. Do something else, pass the binary file and serialize just the file name.
 
First of all, you should not post questions as "solutions". It will only give you abuse reports and down-votes. You should post it as a part of question, via "Improve question".
 
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question.
 
The best method is perhaps the Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].
 
Please see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].
 
The answers are related to .NET, highly recommended. If you don't use any .NET languages, please don't blame me: you did not even mention the language you are using.
 
For C++ (not C++/CLI), for example, I would recommend to look for boost serialization: http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/index.html[^].
 
—SA
  Permalink  
Comments
Abhinav Varma at 18-May-13 1:41am
   
Thanks for your help,
but I am unable to use XmlWriter, and Write objects through it.
 
My Class will look like this :
public class ExClass
{
List<Image> imgList = new List<Image>();
}
 
I could actually store all the images to a folder as StorageFile and retrieve them.
But I want the files NOT to be opened by any others or any other applications.
I want the files, only to be accessed and opened by what the application I write.
 
Can you suggest a method or
would you please write few sample lines code for the above solution you advised, if you dont mind...
(I am a student, and new to .Net)
Sergey Alexandrovich Kryukov at 18-May-13 1:48am
   
What do you mean "unable"? How can I teach you doing programming job in a Quick Question?
If you want to use file exclusively, you can 1) use isolated storage (read about it), 2) keep files open during application lifetime.
This could be a separate unrelated question.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Quote:
So, what are the ways of saving the objects to files in Windows 8 apps(C#).

Have a look at this Tip:
Windows 8 App - Data storage in files using Windows.Storage namespace[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I am getting an Exception:
An exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dll but was not handled in user code
 
Additional information: Type 'Windows.UI.Xaml.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
 
at serializer.WriteObject(stream1, record1)
 
The class is:
[DataContract(Namespace = "http://www.abcdefg.com")]
    internal class Record
    {
        private double n1;
        private BitmapImage bimg;
 
        internal Record(double n1, BitmapImage bimg)
        {
            this.n1 = n1;
            this.bimg = bimg;
        }
        [DataMember]
        internal BitmapImage image
        {
            get { return bimg; }
            set { bimg = value; }
        }
 
        [DataMember]
        internal double OperandNumberOne
        {
            get { return n1; }
            set { n1 = value; }
        }
 
        public override string ToString()
        {
            return string.Format("Record: {0} and an image: {1}", n1, bimg.ToString());
        }
    }
 
and the Serialization is done here:
 
private async void save_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.List;
            openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
            openPicker.FileTypeFilter.Add(".jpg");
            StorageFile file = await openPicker.PickSingleFileAsync();
            IRandomAccessStream imageStream = await file.OpenReadAsync();
            bitmapImage = new BitmapImage();
            bitmapImage.SetSource(imageStream);
            ImgUIelement.Source= bitmapImage;
 
            
            MemoryStream stream1 = new MemoryStream();
            //Serialize the Record object to a memory stream using DataContractSerializer.
            DataContractSerializer serializer = new DataContractSerializer(typeof(Record));
            Record record1 = new Record(1, bitmapImage);
            serializer.WriteObject(stream1, record1);   // EXCEPTION HERE

            tbx_OP.Text = "Written to Stream:" + record1.ToString();
 
            FileSavePicker fileSavePicker = new FileSavePicker();
            fileSavePicker.DefaultFileExtension = ".bla";
            fileSavePicker.FileTypeChoices.Add("Bla Files", new List<string> { ".bla" });
            fileSavePicker.SuggestedFileName = "New Bla File";
            fileSavePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            var sfile = await fileSavePicker.PickSaveFileAsync();
 
            using (Stream x = await sfile.OpenStreamForWriteAsync())
            {
                x.Seek(0, SeekOrigin.Begin);
                stream1.WriteTo(x);
            }
}
</string>
 
My requirement is to serialize an object consisting a List of Images and List of StorageFiles into a File, in my Windows Store App. I am trying to find intially whether it is possible to serialize a Single Image.
If possible could anyone please provide the sample code for a class like this..
public class ExClass
{
    List<Image> imgList = new List<Image>();
    List<StorageFile> audList = new List<StorageFile>();
}
 
I did'nt find any examples for serializing Types other than the Strings,Int,Double..Primitive ones.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 259
1 Maciej Los 240
2 BillWoodruff 240
3 OriginalGriff 220
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 19 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100