问题描述
方法1PublicSharedFunctionDeserializingXml_1(OfT)(ByRefinstanceAsT,ByValpathAsString)AsBooleanTry'生成DataContractSerializerDimserializerAsNewDataContractSerializer(GetType(T))'读入文件UsingfsAsFileStream=NewFileStream(path,FileMode.Open)'---这里报错instance=DirectCast(serializer.ReadObject(fs),T)EndUsingReturnTrueCatchexAsExceptionXmlSerialize.ErrorMessage=ex.MessageReturnFalseEndTryEndFunction
方法2PrivateSubdeserializingXml(OfTypeAsClass)(ByRefrefInstanceAsType,ByValpathAsString)refInstance=NothingDimfsAsFileStream=NothingTryDimsrAsNewSystem.Xml.Serialization.XmlSerializer(GetType(Type))fs=NewFileStream(path,FileMode.Open,FileAccess.Read)TryrefInstance=DirectCast(sr.Deserialize(fs),Type)Finallyfs.Close()EndTryCatchThrowFinallyIfrefInstanceIsNothingThenConsole.WriteLine("Error--->Deserialize")EndIfEndTryEndSub
调用PublicFunctionLoad(OfTypeAsClass)(ByRefinstanceAsType,ByValpathAsString)AsBooleanTry'这里调用方法DeserializingXml(instance,path)CatchReturnFalseEndTryReturnTrueEndFunction
IfpathIsNothingThenfilePath="文件名.xml"ElsefilePath=pathEndIfDimloadDataAsTraceabilityConfig=NothingIfNot(NewXmlSerialize()).Load(loadData,filePath)ThenReturnFalseEndIf
为什么使用方法1会报错--命名空间为空方法2就没问题
解决方案
本帖最后由 neusoft06 于 2014-02-10 15:07:55 编辑