Reputation: 21
Why doesn't the following deserialize? The exception is shown below. Note that I'm using new streams serialization and deserialization to avoid "end of stream" errors. I'm also using new XmlSerializer instances to avoid similar problems there.
Serialization works fine. I've reviewed MANY of the similarly-named questions to no avail.
Exception:
System.InvalidOperationException was unhandled
Message=There is an error in XML document (0, 0).
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at TestSerilalization.Program.Main(String[] args) in D:\MyDocs\Projects\dsb\Src\Apps\RecipeMgr\Other\TestSerlialization\Program.cs:line 45
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.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Xml.XmlException
Message=Root element is missing.
Source=System.Xml
LineNumber=0
LinePosition=0
SourceUri=""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlReader.MoveToContent()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderClass1.Read4_Class1()
InnerException:
Code:
static void Main(string[] args)
{
XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer( typeof( Class1 ), "http://xxx" );
Class1 c1 = new Class1();
FileStream strm = File.Create( "d:\\temp\\classes.xml" );
xmlSerializer.Serialize( strm, c1 );
strm.Close();
Class1 c2 = null;
FileStream strm2 = File.Create( "d:\\temp\\classes.xml" );
XmlSerializer xmlSerializer2 = new System.Xml.Serialization.XmlSerializer( typeof( Class1 ), "http://xxx" );
c2 = (Class1)xmlSerializer2.Deserialize( strm2 );
strm2.Close();
}
public class Class1
{
public Class1()
{
m_Class2 = new Class2();
}
public Class2 class2
{
get { return m_Class2; }
set { m_Class2 = value; }
}
private Class2 m_Class2;
}
public class Class2
{
public Class2()
{
m_Int = 999;
m_Str = "sdfsdfsdsd";
}
public int getInt
{
get { return m_Int; }
set { m_Int = value; }
}
public string getStr
{
get { return m_Str; }
set { m_Str = value; }
}
private int m_Int;
private string m_Str;
}
The generated xml:
<?xml version="1.0"?>
<Class1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://xxx">
<class2>
<getInt>999</getInt>
<getStr>sdfsdfsdsd</getStr>
</class2>
</Class1>
Upvotes: 1
Views: 4595
Reputation: 887479
File.Create
creates a new blank file.
Therefore, you're reading an empty file.
You probably want File.Open
.
Upvotes: 2