SGEN.exe - Microsoft.Xml namespace not found - c#

I have a strange issue with my generated serialization .dll. The .dll is successfully generated I can reference it in my project, all the types are contained within the namespace Microsoft.Xml.Serialization.GeneratedAssembly as expected, even the intellisense is picking up the types and namespaces. But when I go to build the project referencing any of the types within the generated assembly I get an error along the lines of:
Error 2 The type or namespace name 'Xml' does not exist in the
namespace 'Microsoft' (are you missing an assembly reference?)
Rather odd, no? The namespace clearly exists as far as intellisense is concerned and I've never had an issue like this before. I tried to regenerate the assembly, still the same problem.
For now I have run sgen.exe with the flag /keep and just copied the generated source into my project which is fine. I was just curious if there was a way to fix this issue or if anyone else has ever come across it before.
EDIT:
It turns out that the issue is because the generated assembly is targeting a version of the .NET framework greater than the assembly that is referencing it. Now the question becomes - how do I generate a serialization assembly with Sgen that targets a specific .NET framework version.

Ok so after reading the answer on this question I managed to generate a serialization assembly that has the correct "runtime version" for .NET 3.5 and everything works as expected, sorry for wasting time.

Related

"The type or namespace could not be found" error while building a project

I am getting this error while trying to build a solution:
Error CS0246
The type or namespace name 'ClassName' could not be found (are you missing a using directive or an assembly reference?)
The error and the red lines disappear when I open the error list and click on it. I can see the missing dll file is recognized by that class and it is also listed in the references.
I searched many questions here but none of them worked for me. Here is a list of things I tried so far that might help to understand the problem.
I've got the project from TFS and have been never able to build so far. I think the project is not correctly checked in.
I choose 'build only' in the Error list section, after that the errors did not disappear.
All project versions are .Net 4.6, none of them is Client Profile like other in questions here.
I tried to clean, to rebuild, deleting references and adding them again, but none of them worked.
I have changed build output to Diagnostic then inspected errors.
I have found this line and changed all projects target version from 4.6 to 4.6.2 and it worked.
Dll could not be resolved because it was built against the
".NETFramework,Version=v4.6.2" framework. This is a higher version
than the currently targeted framework ".NETFramework,Version=v4.6".

How to solve 'AlprNet' could not be found?

I looked for the answers here and there with no luck, so I decided to ask out.
I want to implement openalpr (open source Automatic License Plate Recognition)
repository on my own PC and I am following this video to accomplish the task. I have done all the steps he did and at the end, when I tried to build it but, in one of the .cs code there is CS0246 error that says:
Error CS0246 The type or namespace name 'AlprNet' could not be found (are you missing a using directive or an assembly reference?) number_plate c:\users\sohib\documents\visual studio 2015\Projects\number_plate\number_plate\Form1.cs
I could only find AlprNet in my D:\Projects\plate_recognition\openalpr-master\src\bindings\csharp\AlprNetTest and it is in .csproj format.
AlprNet is not in .dll format like other references so I cannot reference it in References.
I found some say that it might be because of different versions of .NET Framework platforms. I almost did nothing to check that, because I don't think it's related to .NET
In my mere opinion I should link that AlprNet.csproj to get its content, but I'm not sure. If someone can help get around this issue, I'd be very grateful.
If it's not a .net Assembly (dll) that's referenced and it's not a nuget package, then check that the code isn't expecting the Assembly to already be present in the Global Assembly Cache (If you have access to another developer machine where this works, this is easy to check).
Failing that, adding the .csproj to your .sln and then referencing it sounds like the way to go (provided you have access to that of course!).

How to diagnose an issue with csproj references?

I have an issue with a class being used in another project.
Visual studio is able to add a using statement using Resolve (Ctrl+.), but:
The type or namespace name 'SomeClass' could not be found (are you missing a using directive or an assembly reference?
Resolving this again fully qualifies the type but the namespace in the type path is highlighted as not being found.
The namespace appears also in intellisense and the target class is visible in the Object Browser.
Removing unused namespaces removes the added using statement.
I've tried:
Rebuild.
Confirm project dependency in solution options.
Delete reference and re-add.
Confirm class accessibility (despite namespace not being found).
Checked build mode (Debug/Release).
Restart Visual Studio.
Cleared bin folders in both projects.
Restart machine.
What is the next step to diagnosing this baffling issue?
This is likely to be a .Net framework versioning issue. The class being referenced was probably built using a higher version of the framework.
Try checking your framework versions all align in properties, build settings. A project cannot reference an assembly of a higher framework version.
Last time I had this problem my class was not marked public. Just an idea.
This issue was caused by similar namespaces e.g. Company.Product.Project and Product.Project with:
namespace Company.Product.Project
{
using Product.Project;
The class was in Product.Project, but the compiler was searching for Company.Product.Project.
See: Should 'using' statements be inside or outside the namespace? for more details.

Type or namespace could not be found (SCHEMA added)

A schema was added to a .NET project and was build succesfully. Afterwards the new resource was checked in. Now in another computer cannot be build, states that "Type or namespace could not be found (are you missing an assembly reference?)". Any ideas to overceome this issue?
Sounds like you are missing an assembly which might live outside of your source code repository (e.g., installed under Program Files). This is often the case with third-party code. If you look under References, you might see that there is a missing reference.

"The type or namespace name 'XmlSerializer' could not be found" error when System.Xml.dll is referenced

I've already wasted a few hours on this one:
XmlSerializer serializer;
YES, the using is there, the reference is there, I made the entire solution in VS2010 using .NET 4.0 so it's not any of those things. If I go in Object Explorer I can find the XmlSerializer class I want in the correct namespace but if I try typing the above line in to my code file and compiling I get the dreaded
The type or namespace name 'XmlSerializer' could not be found (are you missing a using directive or an assembly reference?)
warning of death. I don't get it on IntelliSense either. All other threads/websites I've looked on have come up blank or with one of the solutions I've already ruled out. What am I missing?
Cheers
Do you build a Silverlight app?
Silverlight has XmlSerializer defined inside System.Xml.Serialization.dll assembly which is not referenced by default.
This often leads to confusion because other framework versions have it defined in System.Xml.dll.
You need to add System.Xml.Serialization.dll to project references to wire it up.
I had the same problem.
Go to Object Explorer, select XmlSerializer and choose copy. Then, paste into code
This helped me with some weird reason (no there wasn't a typo or anything like that).

Categories

Resources