When I try to serialize types, the application I've been using fails.
Statements like this
XmlSerializer lizer = new XmlSerializer(typeof(MyType));
Production:
System.IO.FileNotFoundException occurred Message="Could not load file or assembly '[Containing Assembly of MyType].XmlSerializers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified." Source="mscorlib" FileName="[Containing Assembly of MyType].XmlSerializers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" FusionLog="" StackTrace: at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
I didn't define any special serializers for my class.
How can I solve this problem?
#1 building
On the other hand, troubleshooting compilation errors is complex. These problems are shown in FileNotFoundException with the following message:
File or assembly name abcdef.dll, or one of its dependencies, was not found. File name: "abcdef.dll" at System.Reflection.Assembly.nLoad( ... ) at System.Reflection.Assembly.InternalLoad( ... ) at System.Reflection.Assembly.Load(...) at System.CodeDom.Compiler.CompilerResults.get_CompiledAssembly()
You may want to know how exceptions that the file does not find relate to instantiating serializer objects, but remember: constructors write C files and try to compile them. The call stack for this exception provides some good information to support this suspicion. XmlSerializer encountered an exception while trying to load an assembly generated by CodeDOM, calling the System.Reflection.Assembly.Load method. This exception does not explain why the assembly XmlSerializer should create does not exist. In general, an assembly does not exist because of a compilation failure, which can occur because, in rare cases, serialization properties generate code that the C compiler cannot compile.
Note that this error can also occur when XmlSerializer is running in a secure environment with an account or a temporary directory that cannot be accessed.
Source: http : //msdn.microsoft.com/en-us/library/aa302290.aspx
#2 building
On the Visual Studio project properties (build page, if I remember correctly), there is an option to say "build serialized assemblies.". An attempt was made to enable [the project that contains the MyType assembly] for build.
#3 building
Your type may refer to the GAC Other assemblies not found in the local bin folder = = >
"Or one of the dependencies. The system cannot find the specified file "
Can you give an example of the type to serialize?
Note: make sure your type implements Serializable.
#4 building
Believe it or not, it's normal behavior. Throw an exception but it's handled by XmlSerializer, so if you ignore it, everything should continue to be normal.
I find this annoying. If you search it, there will be many complaints about this problem. But from what I read, Microsoft is not going to do anything.
If you turn off the first chance exception for that particular exception, you can avoid always getting an exception pop-up while debugging. In Visual Studio, go to debug - > exceptions (or press Ctrl + Alt + E), common language runtime exceptions - > system.io - > system.io.filenotfoundexception.
You can post in the blog C XmlSerializer FileNotFound exception Information about other methods can be found in (Chris sell's tool xmlserializerprocompiler).
#5 building
Custom classes to serialize:
[Serializable] public class TestClass { int x = 2; int y = 4; public TestClass(){} public TestClass(int x, int y) { this.x = x; this.y = y; } public int TestFunction() { return x + y; } }
I attached the code snippet. Maybe it can help you.
static void Main(string[] args) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(TestClass)); MemoryStream memoryStream = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); TestClass domain = new TestClass(10, 3); xmlSerializer.Serialize(xmlWriter, domain); memoryStream = (MemoryStream)xmlWriter.BaseStream; string xmlSerializedString = ConvertByteArray2Str(memoryStream.ToArray()); TestClass xmlDomain = (TestClass)DeserializeObject(xmlSerializedString); Console.WriteLine(xmlDomain.TestFunction().ToString()); Console.ReadLine(); }