3

My application generates a bunch of serialization assemblies on start up. In this case I am getting an error because the XmlSerializer is failing to generate one of the serialization assemblies. I can go and check the temporary code file being generated and I see that it contains invalid characters (just one). This an example from one of the temporary files:

        Reader.MoveToElement();
        if (Reader.IsEmptyElement) {
            Reader.Skip();
$           return o;
        }

As you can see the "$" is an invalid character. This makes csc.exe fails later on.

What could make XmlSerializer generate such a file.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
carlosearm
  • 31
  • 1
  • 1
  • 1
    Please show the steps you are doing to generate the above code that contains the error. That is where the problem is and we will need to see the steps be able to point the problem out to you. – Scott Chamberlain Nov 19 '13 at 20:37
  • My code could be very simple. Something like the following would do: var s = new XmlSerializer(typeof(MyClass)). The detail is in what happens in the background. The XmlSerializer will generate code to write the serializeres need for MyClass. This code get compiled by csc.exe, and the result is an assembly that contains the classes needed to read/write instances of MyClass. My problem is the auto generated code (by XmlSerializer) contains illegal characters and of course CSC fails to compile it. – carlosearm Nov 19 '13 at 21:14
  • Please [make a edit](http://stackoverflow.com/posts/20081478/edit) and show the structure of `MyClass` and show the code between `var s = new XmlSerializer(typeof(MyClass))` and the step that gives you a error with `CSC`. I am sorry, but until you update your question I have to vote to close due to: "Questions concerning problems with code you've written must describe the specific problem — **and include valid code to reproduce it** — in the question itself. See http://SSCCE.org for guidance." You need to show us how to reproduce the issue or we can't help you no matter how much we want to. – Scott Chamberlain Nov 19 '13 at 21:21
  • I am sorry, but my class is irrelevant for this problem. This problem is not tied to any of my classes (or their serializers). The problem does not always occur generating the same serializer, nor the invalid characters are the same. Some times the file contains other invalid character, and it is never in the same place. – carlosearm Nov 19 '13 at 21:27
  • I believe for the serializer to work properly, you must have a default constructor for the class. As such, the invalid character could be a direct result of the code in the classes (or rather lack of default constructor code). This shows the validity of Scott's request for the class code as well as the code that is serializing the class. Ultimately, this could have nothing to do with your issue, but this is my best guess as it was the only issue I ever encountered with the XmlSerializer. – John Bartels Nov 28 '13 at 16:10

1 Answers1

0

I would suggest to give the SGEN tool a try - http://msdn.microsoft.com/en-us/library/bk3w6240(v=vs.100).aspx

The tool will take an assembly and generate the corresponding XmlSerializer assembly, which you have problem generating at runtime.

The SGEN tool might provide you with some additional information as to why you experience this problem - compilation errors and warnings.

Another nice feature of using SGEN is that your first xml serialisation, will be much faster, as the application no longer need to do the run-time compilation of the XML structures, but simply loads the XmlSerializer DLL.

Hope this brings you closer to idea on what goes wrong.

ahybertz
  • 514
  • 4
  • 9