locked
How to save list of objects to xml RRS feed

  • Question

  • User2029810015 posted

    I have class

     
    public class ControlDetail
        {
            
            public Type ControlType { get; set; }
            public string Name { get; set; }
            public Point position { get; set; }
            public Size size { get; set; }
            public ControlDetail() { }
        }
    
           public List<ControlDetail> ListControl = null;
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                ListControl = new List<ControlDetail>();
                foreach(Control ctr in Myform.Controls)
                {
                    ControlDetail firstControl = new ControlDetail();
                    firstControl.ControlType = ctr.GetType();
                    firstControl.Name = ctr.Name;
                    firstControl.position =new Point(ctr.Location.X,ctr.Location.Y);
                    firstControl.size = ctr.Size;
                    ListControl.Add(firstControl);
                }
                Type t = typeof(List<ControlDetail>);
                XmlSerializer serialiser = new XmlSerializer(t.GetType());//Getting Error at this line
    
                // Create the TextWriter for the serialiser to use
                TextWriter Filestream = new StreamWriter(Application.StartupPath+"\\prakash.xml");
    
                //write to the file
                serialiser.Serialize(Filestream, ListControl);
                //serialiser.Serialize()
    
            }
    
            
    
            
        }



    Friday, September 13, 2013 7:31 AM

Answers

  • User-760709272 posted

    What is it you're trying to serialise?  Is it a control?  If so you won't be able to serialoise it.  To ensure it's not your code, try serialising a simple class with basic native type properties.  If that is working and your code is ok, you need to mark properties that can't be serialised with [XmlIgnore].  If you are serialising something that you can't update the source for then you're out of luck - not everything can be serialised to xml.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 13, 2013 7:49 AM

All replies

  • User-760709272 posted
    XmlSerializer serialiser = new XmlSerializer(t);//Getting Error at this line
    

    Friday, September 13, 2013 7:39 AM
  • User2029810015 posted

    now getting error on

    This line
    serialiser.Serialize(Filestream, ListControl); 
    
    and details are below
    
    System.InvalidOperationException was unhandled
      HResult=-2146233079
      Message=There was an error generating the XML document.
      Source=System.Xml
      StackTrace:
           at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
           at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
           at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
           at Screens_ProtoType.Form1.Form1_FormClosing(Object sender, FormClosingEventArgs e) in C:\Users\allgreen2\Documents\Visual Studio 2010\Projects\Screens_ProtoType\Screens_ProtoType\Form1.cs:line 87
           at System.Windows.Forms.Form.OnFormClosing(FormClosingEventArgs e)
           at System.Windows.Forms.Form.WmClose(Message& m)
           at System.Windows.Forms.Form.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DefFrameProc(IntPtr hWnd, IntPtr hWndClient, Int32 msg, IntPtr wParam, IntPtr lParam)
           at System.Windows.Forms.Form.DefWndProc(Message& m)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           at System.Windows.Forms.ContainerControl.WndProc(Message& m)
           at System.Windows.Forms.Form.WmSysCommand(Message& m)
           at System.Windows.Forms.Form.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DefFrameProc(IntPtr hWnd, IntPtr hWndClient, Int32 msg, IntPtr wParam, IntPtr lParam)
           at System.Windows.Forms.Form.DefWndProc(Message& m)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           at System.Windows.Forms.ContainerControl.WndProc(Message& m)
           at System.Windows.Forms.Form.WmNcButtonDown(Message& m)
           at System.Windows.Forms.Form.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at Screens_ProtoType.Program.Main() in C:\Users\allgreen2\Documents\Visual Studio 2010\Projects\Screens_ProtoType\Screens_ProtoType\Program.cs:line 18
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: System.InvalidOperationException
           HResult=-2146233079
           Message=System.RuntimeType is inaccessible due to its protection level. Only public types can be processed.
           Source=System.Xml
           StackTrace:
                at System.Xml.Serialization.TypeDesc.CheckSupported()
                at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
                at System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type type)
                at System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Object o)
                at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write3_Type(String n, String ns, Type o, Boolean isNullable, Boolean needType)
                at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write6_ControlDetail(String n, String ns, ControlDetail o, Boolean isNullable, Boolean needType)
                at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write7_ArrayOfControlDetail(Object o)
           InnerException: 
    



    Friday, September 13, 2013 7:42 AM
  • User-760709272 posted

    What is it you're trying to serialise?  Is it a control?  If so you won't be able to serialoise it.  To ensure it's not your code, try serialising a simple class with basic native type properties.  If that is working and your code is ok, you need to mark properties that can't be serialised with [XmlIgnore].  If you are serialising something that you can't update the source for then you're out of luck - not everything can be serialised to xml.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 13, 2013 7:49 AM