Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<?xml version="1.0" encoding="UTF-8"?>
<Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>4</FirstName>
  <FamilyName>4</FamilyName>
  <UserLevel>عاد?</UserLevel>
  <Password>4</Password>
</Address>

Addresses =(AddressBook) SerializableData.Load(DataFileName,typeof(AddressBook));

""""""""""DataFileName is AddressBook.xml""""""""


public static Object Load(Stream stream, Type newType)
       {
           // create a serializer and load the object....
           XmlSerializer serializer = new XmlSerializer(newType);
           Object newObject = serializer.Deserialize(stream);
           // return the new object...
           return newObject;
       }



I have this problem and I cant solve it. Stack trace :

at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)\r\n<br />
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)\r\n<br />
at AddressBook.Module.SerializableData.Load(Stream stream, Type newType) in C:\\ExCs\\ExCs\\Classes\\SerializableData.cs:line 38\r\n<br />
 at AddressBook.Module.SerializableData.Load(String filename, Type newType) in C:\\ExCs\\ExCs\\Classes\\SerializableData.cs:line 28\r\n<br />
 at ExCs.UserManager.UserManager_Load_1(Object sender, EventArgs e) in C:\\ExCs\\ExCs\\Modules\\UserManager.cs:line 214\r\n<br />
at System.Windows.Forms.UserControl.OnLoad(EventArgs e)\r\n<br />
at System.Windows.Forms.UserControl.OnCreateControl()\r\n<br />
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)\r\n<br />
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)\r\n<br />
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)\r\n<br />
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)\r\n<br />
at System.Windows.Forms.Control.CreateControl()\r\n   at System.Windows.Forms.Control.WmShowWindow(Message& m)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)\r\n<br />
at System.Windows.Forms.ContainerControl.WndProc(Message& m)\r\n<br />
at System.Windows.Forms.Form.WmShowWindow(Message& m)\r\n   at System.Windows.Forms.Form.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n<br />
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n<br />
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n<br />
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)\r\n<br />
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)\r\n<br />
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)\r\n<br />
at System.Windows.Forms.Control.set_Visible(Boolean value)\r\n<br />
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.Run(Form mainForm)\r\n<br />
at ExCs.Program.Main() in C:\\ExCs\\ExCs\\Classes\\Program.cs:line 18\r\n<br />
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n<br />
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n<br />
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n<br />
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n<br />
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n<br />
at System.Threading.ThreadHelper.ThreadStart()"
Posted
Updated 26-Feb-12 3:07am
v3
Comments
#realJSOP 26-Feb-12 9:19am    
WHAT is the exact error, and WHERE is it happening.
Show us the code for the object you're trying to serialize to/from.
maria anders 26-Feb-12 9:26am    
my error is what i mentioned,"There is an error in XML document (2, 2)"
and it happen when i want to decenteralize
***
Object newObject = serializer.Deserialize(stream);
****
maria anders 26-Feb-12 9:28am    
this part of code is the code i redfer to this line

FileInfo fileInfo = new FileInfo(filename);
if(!fileInfo.Exists)
{
// create a blank version of the object and return that...
return System.Activator.CreateInstance(newType);
}
// open the file...
FileStream stream = new FileStream(filename, FileMode.Open);
// load the object from the stream...
Object newObject = Load(stream, newType);
// close the stream...
stream.Close();
// return the object...
return newObject;

1 solution

Apologies for my previous incorrect assumption.

C#
Addresses =(AddressBook) SerializableData.Load(DataFileName,typeof(AddressBook));

You are trying to load a type of AddressBook but the XML contains an element that is an Address so it does not match.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900