views:

61

answers:

1

i try to use xml serilization in gridview but it returns me: InvalidOperationException was unhandled by user code

My code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using System.IO;


namespace WebAppl_serilization
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FileStream myStream = new FileStream("GridView.XML", FileMode.Create);

            XmlSerializer myXs = new XmlSerializer(typeof(GridView));

            myXs.Serialize(myStream, gv);


            myStream.Close();
            Console.WriteLine("Devam etmek için bir tuşa basın");
            Console.ReadLine();

            FileStream mySecondFS = new FileStream("GridView.XML", FileMode.Open);
            XmlSerializer mySecondXs = new XmlSerializer(typeof(GridView));
            GridView test = (GridView)mySecondXs.Deserialize(mySecondFS);
            mySecondFS.Close();
            //foreach (GridView grd in test)
            //{
            //    Console.Write(grd.);
            //}
            Console.ReadKey();
        }
    }
}

i t throw me: XmlSerializer myXs = new XmlSerializer(typeof(GridView)); error message: InvalidOperationException was unhandled by user code Detail: 'System.Web.UI.WebControls.GridView' was an error reflecting type.

my html source:

   <div>
    <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField DataField="Kitap" HeaderText="Kitap" />
    </Columns>
    </asp:GridView>
    </div>
+1  A: 

I doubt the GridView is serializable. A better option would be to serialize the data, not the GridView.

Patrick Steele