2016-08-16 86 views
1

我想序列化3列表到一個獨特的XML文件。 我的項目是一個UWP,我寫了一個方法,但它目前僅序列化1個列表。 我怎樣才能用我的方法序列化3列表。c#序列化多個1列表在同一個xml文件

我現在的序列化方法:

public static async void SaveObjectToXml<T>(T objectToSave) 
{ 
    //<-----------FilePicker------------------> 
    var savePicker = new Windows.Storage.Pickers.FileSavePicker(); 
    savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; 
    savePicker.FileTypeChoices.Add("New File", new List<string>() { ".xml" }); 
    savePicker.SuggestedFileName = "New File"; 
    StorageFile newfile = await savePicker.PickSaveFileAsync(); 
    //<-----------FilePicker------------------> 
    //<----------------Serializacion------------------> 

    var serializer = new XmlSerializer(typeof(T)); 

    Stream stream = await newfile.OpenStreamForWriteAsync(); 


    using (stream) 
    { 
     serializer.Serialize(stream, objectToSave); 
    } 
    //<----------------Serializacion------------------> 

} 

我的列表:

public class List1 : BindableBase 
{   
    private int _Property1; 
    public int Property1 
    { 
     get { return _Property1; } 
     set { SetProperty(ref _Property1, value); } 
    } 

    private bool _Property2; 
    public bool Property2 
    { 
     get { return _Property2; } 
     set { SetProperty(ref _Property2, value); } 
    } 

    private bool _Property3; 
    public bool Property3 
    { 
     get { return _Property3; } 
     set { SetProperty(ref _Property3, value); } 
    } 
} 




public class List2 : BindableBase 
{   

    private bool _Property1; 
    public bool Property1 
    { 
     get { return _Property1; } 
     set { SetProperty(ref _Property1, value); } 
    } 

    private bool _Property2; 
    public bool Property2 
    { 
     get { return _Property2; } 
     set { SetProperty(ref _Property2, value); } 
    } 
} 

    public class List3 : BindableBase 
{   
    private double _Property1; 
    public double Property1 
    { 
     get { return _Property1; } 
     set { SetProperty(ref _Property1, value); } 
    } 

    private double _Property2; 
    public double Property2 
    { 
     get { return _Property2; } 
     set { SetProperty(ref _Property2, value); } 
    } 

    private double _Property3; 
    public double Property3 
    { 
     get { return _Property3; } 
     set { SetProperty(ref _Property3, value); } 
    } 

    private double _Property4; 
    public double Property4 
    { 
     get { return _Property4; } 
     set { SetProperty(ref _Property4, value); } 
    } 

} 

public class Values_List1 : ObservableCollection<List1> 
    { 
    } 
    public class Values_List2 : ObservableCollection<List2> 
    { 
    } 
    public class Values_List3 : ObservableCollection<List3> 
    { 
    } 

public static class ApplicationServices 
    { 
public static Values_List1 List1 = new Values_List1(); 
public static Values_List2 List2 = new Values_List2(); 
public static Values_List3 List3 = new Values_List3(); 

static ApplicationServices() 
     { 
     } 
    } 


List1.Add(new List1() 
      { 
       Property1 = 1, 
       Property2 = true, 
       Property3 = false, 
      }); 


List2.Add(new List2() 
      {     
       Property1 = true, 
       Property2 = false, 
      }); 


List3.Add(new List3() 
      { 
       Property1 = 3.564, 
       Property2 = 0.215, 
       Property3 = 0.7252, 
       Property3 = 23.463, 
      }); 

I wanna get something similar to this:

<?xml version="1.0" encoding="utf-8"?> 
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <List1> 
    <Property1>1</Property1> 
    <Property2>true</Property2> 
    <Property3>false</Property3> 
    <ToAcero>0.1</ToAcero> 
    </List1> 
    <List2> 
    <Property1>true</Property1> 
    <Property2>true</Property2> 
    </List2> 
    <List3> 
    <Property1>3.564</Property1> 
    <Property2>0.215</Property2> 
    <Property3>0.7252</Property3> 
    <Property4>23.463</Property4> 
    </List3> 
</Root> 

任何幫助表示讚賞。

回答

1

你應該有這樣的課程;

class myCompoundClass 
{ 
    public List1 List1 {get;set;} 
    public List2 List2 {get;set;} 
    public List3 List3 {get;set;} 

} 

並序列化myCompoundClass實例。

+1

對於我來說,將列表序列化爲數組而不是作爲具有屬性的包裝器對象似乎更符合邏輯。 – serhiyb

+0

謝謝,我這樣做,但我的方法返回錯誤:「myCompoundClass是一種類型,在給定的上下文中無效。」 – Julius

+0

@Julius您可以在使用此解決方案的位置發佈代碼嗎? –