XML 對象反序列化也動態編譯? - asp.net
編輯:XML詳解  
作者:佚名 文章來源:不詳 點擊數:
3 更新時間:2007-4-9
的時候從 XML 文件反序列化一個對象。一般情況下都是好,極少數情況下會出現一下問題。
System.Runtime.InteropServices.ExternalException: Timed out waiting for a program to execute. The command being executed was "c:\Windows\microsoft.Net\framework\v1.1.4322\csc.exe" /noconfig @"C:\DOCUME~1\montaq~1\LOCALS~1\Temp\prfmes01.cmdline".
at System.CodeDom.Compiler.Executor.ExecWaitWithCaptureUnimpersonated(IntPtr userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine)
at System.CodeDom.Compiler.Executor.ExecWaitWithCapture(IntPtr userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine)
at System.CodeDom.Compiler.CodeCompiler.Compile(CompilerParameters options, String compilerDirectory, String compilerExe, String arguments, String& outputFile, Int32& nativeReturnValue, String trueArgs)
at System.CodeDom.Compiler.CodeCompiler.FromFileBatch(CompilerParameters options, String[] fileNames)
at System.CodeDom.Compiler.CodeCompiler.FromSourceBatch(CompilerParameters options, String[] sources)
at System.CodeDom.Compiler.CodeCompiler.FromSource(CompilerParameters options, String source)
at System.CodeDom.Compiler.CodeCompiler.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSource(CompilerParameters options, String source)
at System.XML.Serialization.Compiler.Compile()
at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] XMLMappings)
at System.Xml.Serialization.XMLSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XMLSerializer..ctor(Type type)
at XMLState..ctor(String path)
at AutoUpdateingAddin.Updater..ctor(String hostAppExeFullPath)
at AutoUpdateingAddin.Form1.CheckUpdate()
at AutoUpdateingAddin.Form1.Form1_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(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.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
XML 文件不便,程序也不變,有時候就會有這樣的錯誤。
從這個錯誤看出來,對象反序列化的時候,會調用動態編譯,首先動態的生成一些代碼,然後再去編譯執行。hoho,中間結果都放在temp文件中。執行結束的時候,delete他。
用reflector 驗證了一下,果然如此。(y)
為了測試,我寫一個簡單的反序列化程序
新建一個類
using System;
using System.XML;
using System.XML.Serialization;
namespace XMLDemo
{
[XMLRoot("Person",Namespace="")]
public class Person
{
[XMLElement("PersonName")]
public string Name;
public int Age;
}
}
然後我生成一個該類的實例並把它寫道XML 文件。文件內容如下
<?XML version="1.0" ?>
- <Person xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PersonName>Montaque</PersonName>
<Age>30</Age>
</Person> 接下來就是反序列化這個類,記住要引用 System.runtime.Serialization.dll
代碼如下 System.Xml.Serialization.XmlSerializer xr=new System.Xml.Serialization.XMLSerializer(typeof(Person)) ;
System.IO.FileStream fs=new System.IO.FileStream("c:\\test.XML",System.IO.FileMode.Open ,System.IO.FileAccess.Read);
Person p =(Person)xr.Deserialize(fs);
Console.WriteLine(p.Name);
運行結果沒有問題。
運行過程中,我用文件監視器堅實了一下csc.exe ,果然他先生成一下中間的cs,然後編譯。。。