locked
Read List<Type> Using DataContractSerializer from StorageFile RRS feed

  • Question

  • In the code below, I'm good until the last line. I have an xml file that was created in a Windows 7 project that uses classes as containers (Data1 - Data5). I can open my xml file as a StorageFile, so all I thought I'd have to do is use DataContractSerializer to read the xml representing the 5 different objects within the file. I've found metro examples where they have inputStream.AsStreamForRead(). I don't have AsStreamForRead() as an option. I'm using Windows 8 (not 8.1) and Visual Studio 2012.

    List<Type> listOfTypes = new List<Type>
        {
            typeof(Data1), typeof(Data2), typeof(Data3), 
                typeof(Data4), typeof(Data5)
        };
    DataContractSerializer serializer =
            new DataContractSerializer(typeof(TotalDataQuery), listOfTypes);
    
    IInputStream inputStream = await storageFile.OpenReadAsync();
    
    var x = serializer.ReadObject(inputStream);


    Randy

    Monday, October 28, 2013 7:06 PM

Answers

  • Hi, Randy

    I create a XML file which uses classes as containers (Data1 - Data5). I try to use DataContractSerializer to deserialize the xml file. There are some codes you can try:

    XML file:

    <?xml version="1.0" encoding="utf-8" ?>
    <Datas>
      <Data1>
        <name>ddd</name>
        <title>dttt</title>
      </Data1>
      <Data2>
        <full>rrtf</full>
        <easy>ghhj</easy>
      </Data2>
      <Data3>
        <user>rttt</user>
        <classs>tttty</classs>
      </Data3>
      <Data4>
        <score>tt</score>
      </Data4>
      <Data5>
        <school>yy</school>
      </Data5>
    </Datas>
    

    Codes:

     [DataContract(Name="Datas",Namespace = "")]
        public class Datas
        {
            
            [DataMember(Name="Data1",Order=0)]
            public Data1 data1{get;set;}
        
            [DataMember(Name = "Data2", Order = 1)]
            public Data2 data2{get;set;}
           
            [DataMember(Name = "Data3", Order = 2)]
            public Data3 data3 { get; set; }
           
            [DataMember(Name = "Data4", Order = 3)]
            public Data4 data4 { get; set; }
            
            [DataMember(Name = "Data5", Order = 4)]
            public Data5 data5{ get; set; }
            
    
        }
         [DataContract(Name = "Data1", Namespace = "")]
        public class Data1
        {
             
             [DataMember(Name="name",Order=0)]
            public string name { get; set; }
             
             [DataMember(Name="title",Order=1)]
            public string title { get; set; }
             public Data1(string me, string tle)
             {
                 this.name = me;
                 this.title = tle;
             }
    
        }
        [DataContract(Name = "Data2", Namespace = "")]
        public class Data2
        {
            [DataMember(Name = "full", Order = 0)]
            public string full { get; set; }
            [DataMember(Name = "easy", Order = 1)]
            public string easy { get; set; }
            public Data2(string ful,string ead)
            {
                this.full = ful;
                this.easy = ead;
            }
        }
       [DataContract(Name = "Data3", Namespace = "")]
        public class Data3
        {
            [DataMember(Name = "user", Order = 0)]
            public string user { get; set; }
            [DataMember(Name = "classs", Order = 1)]
            public string classs { get; set; }
            public Data3(string us,string cla)
            {
                this.user = us;
                this.classs = cla;
            }
        }
          [DataContract(Name = "Data4", Namespace = "")]
        public class Data4
        {
            [DataMember(Name = "score", Order = 0)]
            public string score { get; set; }
            public Data4(string sco)
            {
                this.score = sco;
            }
        }
          [DataContract(Name = "Data5", Namespace = "")]
        public class Data5
        {
            [DataMember(Name = "school", Order = 0)]
            public string school { get; set; }
            public Data5(string sch)
            {
                this.school = sch;
            }
        }
    
     private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
               
              List<Type> listOfTypes = new List<Type>
        {
            typeof(Data1), typeof(Data2), typeof(Data3), 
                typeof(Data4), typeof(Data5)
        };
                DataContractSerializer serializer =
            new DataContractSerializer(typeof(Datas),listOfTypes);
                StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
                StorageFile file1 = await storageFolder.CreateFileAsync("XMLFile1.xml",CreationCollisionOption.OpenIfExists);
                Stream sessionRandomAccess = await file1.OpenStreamForReadAsync();
                //IInputStream sessioninputStream = sessionRandomAccess.GetInputStreamAt(0);
                var datas = serializer.ReadObject(sessionRandomAccess);
    
    
            }
        }
    Best Wishes!



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, October 29, 2013 8:57 AM

All replies

  • I sorted it out. I needed a Stream, not an IInputStream. Thanks for letting me vent...

    List<Type> listOfTypes = new List<Type>
        {
            typeof(Data1), typeof(Data2), typeof(Data3), 
                typeof(Data4), typeof(Data5)
        };
    DataContractSerializer serializer =
            new DataContractSerializer(typeof(TotalDataQuery), listOfTypes);
    
    Stream stream = await storageFile.OpenStreamForReadAsync();
    TotalDataQuery query = (TotalDataQuery)serializer.ReadObject(stream);


    Randy

    Monday, October 28, 2013 8:03 PM
  • Hi, Randy

    I create a XML file which uses classes as containers (Data1 - Data5). I try to use DataContractSerializer to deserialize the xml file. There are some codes you can try:

    XML file:

    <?xml version="1.0" encoding="utf-8" ?>
    <Datas>
      <Data1>
        <name>ddd</name>
        <title>dttt</title>
      </Data1>
      <Data2>
        <full>rrtf</full>
        <easy>ghhj</easy>
      </Data2>
      <Data3>
        <user>rttt</user>
        <classs>tttty</classs>
      </Data3>
      <Data4>
        <score>tt</score>
      </Data4>
      <Data5>
        <school>yy</school>
      </Data5>
    </Datas>
    

    Codes:

     [DataContract(Name="Datas",Namespace = "")]
        public class Datas
        {
            
            [DataMember(Name="Data1",Order=0)]
            public Data1 data1{get;set;}
        
            [DataMember(Name = "Data2", Order = 1)]
            public Data2 data2{get;set;}
           
            [DataMember(Name = "Data3", Order = 2)]
            public Data3 data3 { get; set; }
           
            [DataMember(Name = "Data4", Order = 3)]
            public Data4 data4 { get; set; }
            
            [DataMember(Name = "Data5", Order = 4)]
            public Data5 data5{ get; set; }
            
    
        }
         [DataContract(Name = "Data1", Namespace = "")]
        public class Data1
        {
             
             [DataMember(Name="name",Order=0)]
            public string name { get; set; }
             
             [DataMember(Name="title",Order=1)]
            public string title { get; set; }
             public Data1(string me, string tle)
             {
                 this.name = me;
                 this.title = tle;
             }
    
        }
        [DataContract(Name = "Data2", Namespace = "")]
        public class Data2
        {
            [DataMember(Name = "full", Order = 0)]
            public string full { get; set; }
            [DataMember(Name = "easy", Order = 1)]
            public string easy { get; set; }
            public Data2(string ful,string ead)
            {
                this.full = ful;
                this.easy = ead;
            }
        }
       [DataContract(Name = "Data3", Namespace = "")]
        public class Data3
        {
            [DataMember(Name = "user", Order = 0)]
            public string user { get; set; }
            [DataMember(Name = "classs", Order = 1)]
            public string classs { get; set; }
            public Data3(string us,string cla)
            {
                this.user = us;
                this.classs = cla;
            }
        }
          [DataContract(Name = "Data4", Namespace = "")]
        public class Data4
        {
            [DataMember(Name = "score", Order = 0)]
            public string score { get; set; }
            public Data4(string sco)
            {
                this.score = sco;
            }
        }
          [DataContract(Name = "Data5", Namespace = "")]
        public class Data5
        {
            [DataMember(Name = "school", Order = 0)]
            public string school { get; set; }
            public Data5(string sch)
            {
                this.school = sch;
            }
        }
    
     private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
               
              List<Type> listOfTypes = new List<Type>
        {
            typeof(Data1), typeof(Data2), typeof(Data3), 
                typeof(Data4), typeof(Data5)
        };
                DataContractSerializer serializer =
            new DataContractSerializer(typeof(Datas),listOfTypes);
                StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
                StorageFile file1 = await storageFolder.CreateFileAsync("XMLFile1.xml",CreationCollisionOption.OpenIfExists);
                Stream sessionRandomAccess = await file1.OpenStreamForReadAsync();
                //IInputStream sessioninputStream = sessionRandomAccess.GetInputStreamAt(0);
                var datas = serializer.ReadObject(sessionRandomAccess);
    
    
            }
        }
    Best Wishes!



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, October 29, 2013 8:57 AM