none
C## Express 2010 XmlSerializer 複数のBindingListを1つのファイルに保存したい。 RRS feed

  • 質問

  • いつもお世話になっております。
    よろしくお願いいたします。

    目的
    文字列の1次元配列を、DataGridViewで表示させつつ編集し、保存及び次回起動時に復元したい。 

    現状
    当初はDataGridView にDatasetをDatasourceしていました。理由はXmlファイルでの保存及び復元が楽だったからです。しかし、先日それではオーバースペックだというご指摘を頂戴し、いまでは、BindingList<CMyClass>をDatasourceしています。Listでは-1エラーっていうのが頻発してとんちんかんでしたが、BindingListにしたら一気にうまくいったのを覚えています。
    さて、BindingListインスタンスを保存及び復元したい段階にきているのですが、今回はXmlシリアライザを使ってみようと考えています。そして、現に、保存及び復元することができました。

    質問
    BindingList<CMyClass>[]を宣言し、配列数は3つ用意しています。リストの配列ということです。

    BindingList<CElement>[] blist;
    
    public MainForm()
        {
          InitializeComponent();
          yDataGridView = new[] { dataGridView_desc, dataGridView_back, dataGridView_claim };
          yDataGridView[1].AlternatingRowsDefaultCellStyle = yDataGridView[0].AlternatingRowsDefaultCellStyle;
          yDataGridView[2].AlternatingRowsDefaultCellStyle = yDataGridView[0].AlternatingRowsDefaultCellStyle;
          yDataGridView[1].DefaultCellStyle = yDataGridView[0].DefaultCellStyle;
          yDataGridView[2].DefaultCellStyle = yDataGridView[0].DefaultCellStyle;
    
          blist = new BindingList<CElement>[3];
          for (int i = 0; i < 3; i++)
          {
            blist[i] = new BindingList<CElement>();
          }
    
          dataGridView_desc.DataSource = blist[0];
          dataGridView_back.DataSource = blist[1];
          dataGridView_claim.DataSource = blist[2];
    
          strFullpathOfWord = null;
        }
    
     private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
          //アプリケーション設定の保存
          Properties.Settings.Default.intFormWidth = this.Width;
          Properties.Settings.Default.Save();
          
          System.Xml.Serialization.XmlSerializer serializer1 = new System.Xml.Serialization.XmlSerializer(typeof(BindingList<CElement>[]));
          System.IO.FileStream fs1;
    
          fs1 = new System.IO.FileStream(@"C:\Documents and Settings\test\デスクトップ\test.xml", System.IO.FileMode.Create);
          serializer1.Serialize(fs1, blist);
          fs1.Close();
        }
    

     実行し、フォームを閉じると、

    一時クラスを生成できません (result=1)。

    error CS1026: ) が必要です。

    error CS1002: ; が必要です。

    error CS1525: ')' は無効です。

    error CS1002: ; が必要です。

    error CS1525: ')' は無効です。

    error CS1002: ; が必要です。

    error CS1026: ) が必要です。

    error CS1002: ; が必要です。

    error CS1525: ')' は無効です。

    error CS1002: ; が必要です。

    error CS1525: ')' は無効です。

    error CS1002: ; が必要です。

    という内容のInvalidOperationExceptionがハンドルされなかったとのエラーが出てしまいます。
    何か根本的に全くおかしなことをやっているような気がするのですが、いかがでしょうか。 

    2011年4月19日 4:44

回答

すべての返信

  • XmlSerializer の仕様として、ArrayList および List<T> の配列はシリアライズできません。

    参照:XmlSerializer クラス (System.Xml.Serialization)

    おそらく BindingList が内部で List<T> を使っているのでそれをシリアライズできないのでしょう。

    代わりに List<BindingList<T>> を使用するなどしてください。

    • 回答としてマーク bianoide 2011年4月19日 7:14
    2011年4月19日 4:59
  • アドバイスありがとうございます。

    ちょっと脱線してすみませんが、Hongliangさんの名前をみたとき、
    なぜか、グローバルフックのことを思い出しました。なぜだかいまいち思い出せないのですが・・・ 

    2011年4月19日 5:29
  • List<BindingList<T>>に切り替えたらすんなりうまく行きました!
    ありがとうございました。bianoide

     

     <?xml version="1.0" ?> 
    - <ArrayOfArrayOfCElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    - <ArrayOfCElement>
    - <CElement>
     <Name>hello1</Name> 
     </CElement>
    - <CElement>
     <Name>hello1</Name> 
     </CElement>
    - <CElement>
     <Name>hello1</Name> 
     </CElement>
    - <CElement>
     <Name>hello1</Name> 
     </CElement>
    - <CElement>
     <Name>hi1</Name> 
     </CElement>
    - <CElement>
     <Name>hi1</Name> 
     </CElement>
    - <CElement>
     <Name>hi1</Name> 
     </CElement>
    - <CElement>
     <Name>hello1</Name> 
     </CElement>
    - <CElement>
     <Name>hello1</Name> 
     </CElement>
    - <CElement>
     <Name>hello1</Name> 
     </CElement>
    - <CElement>
     <Name>hello1</Name> 
     </CElement>
    - <CElement>
     <Name>hi1</Name> 
     </CElement>
    - <CElement>
     <Name>hi1</Name> 
     </CElement>
    - <CElement>
     <Name>hi1</Name> 
     </CElement>
     </ArrayOfCElement>
    - <ArrayOfCElement>
    - <CElement>
     <Name>hello2</Name> 
     </CElement>
    - <CElement>
     <Name>hello2</Name> 
     </CElement>
    - <CElement>
     <Name>hello2</Name> 
     </CElement>
    - <CElement>
     <Name>hello2</Name> 
     </CElement>
    - <CElement>
     <Name>hi2</Name> 
     </CElement>
    - <CElement>
     <Name>hi2</Name> 
     </CElement>
    - <CElement>
     <Name>hi2</Name> 
     </CElement>
    - <CElement>
     <Name>hello2</Name> 
     </CElement>
    - <CElement>
     <Name>hello2</Name> 
     </CElement>
    - <CElement>
     <Name>hello2</Name> 
     </CElement>
    - <CElement>
     <Name>hello2</Name> 
     </CElement>
    - <CElement>
     <Name>hi2</Name> 
     </CElement>
    - <CElement>
     <Name>hi2</Name> 
     </CElement>
    - <CElement>
     <Name>hi2</Name> 
     </CElement>
     </ArrayOfCElement>
    - <ArrayOfCElement>
    - <CElement>
     <Name>hello3</Name> 
     </CElement>
    - <CElement>
     <Name>hello3</Name> 
     </CElement>
    - <CElement>
     <Name>hello3</Name> 
     </CElement>
    - <CElement>
     <Name>hello3</Name> 
     </CElement>
    - <CElement>
     <Name>hi3</Name> 
     </CElement>
    - <CElement>
     <Name>hi3</Name> 
     </CElement>
    - <CElement>
     <Name>hi3</Name> 
     </CElement>
    - <CElement>
     <Name>hello3</Name> 
     </CElement>
    - <CElement>
     <Name>hello3</Name> 
     </CElement>
    - <CElement>
     <Name>hello3</Name> 
     </CElement>
    - <CElement>
     <Name>hello3</Name> 
     </CElement>
    - <CElement>
     <Name>hi3</Name> 
     </CElement>
    - <CElement>
     <Name>hi3</Name> 
     </CElement>
    - <CElement>
     <Name>hi3</Name> 
     </CElement>
     </ArrayOfCElement>
     </ArrayOfArrayOfCElement>
    
    2011年4月19日 7:13
  • とりあえず現状のソース抜粋です。ありがとうございました。
     DataGridView[] yDataGridView;
        List<BindingList<CElement>> listblist;
        public string strFullpathOfWord;
    
        public MainForm()
        {
          InitializeComponent();
          yDataGridView = new[] { dataGridView_desc, dataGridView_back, dataGridView_claim };
          yDataGridView[1].AlternatingRowsDefaultCellStyle = yDataGridView[0].AlternatingRowsDefaultCellStyle;
          yDataGridView[2].AlternatingRowsDefaultCellStyle = yDataGridView[0].AlternatingRowsDefaultCellStyle;
          yDataGridView[1].DefaultCellStyle = yDataGridView[0].DefaultCellStyle;
          yDataGridView[2].DefaultCellStyle = yDataGridView[0].DefaultCellStyle;
    
          listblist = new List<BindingList<CElement>>();
          listblist.Add(new BindingList<CElement>());
          listblist.Add(new BindingList<CElement>());
          listblist.Add(new BindingList<CElement>());
    
          dataGridView_desc.DataSource = listblist[0];
          dataGridView_back.DataSource = listblist[1];
          dataGridView_claim.DataSource = listblist[2];
    
          strFullpathOfWord = null;
        }
    
    2011年4月19日 7:15