Why can't I reference System.Runtime.Serialization.Json in C# - c#

I want to use an API to get info from the interwebz. The API returns data in Json format.
I'm running Microsoft Visual Studio C# 2010 Express addition.
It appears that I have the .NET Framework 4 Client Profile set as my
"Target framework" but I'm honestly not sure exactly what this
means.
This is a Windows Forms Application...
Not much code to show because I can't really get started without the appropriate using statement...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Net;
using System.Runtime.Serialization.Json;
I get this error:
The type or namespace name 'Json' does not exist in the namespace
'System.Runtime.Serialization' (are you missing an assembly
reference?)
Am I missing a DLL file or something? Based on my hours of fruitlessly searching for solutions, I understand that the .NET 4.xx should already have the tools needed to parse up a Json formatted string?

The System.Runtime.Serialization.Json Namespace is in two different DLL's depending on your .net framework.
In .NET 3.5 It is in System.ServiceModel.Web.dll
In .NET 4.0 and above It is in System.Runtime.Serialization.dll.
Make sure you have added the correct DLL as a reference in your project and add using System.Runtime.Serialization.Json; to the top of your code file.
EDIT - Consider using JSON.NET
Even though the .NET Framework supplies its own JSON Serialization and Deserialization namespaces (DataContractJsonSerializer and JavaScriptSerializer) you should investigate whether you would be better off using JSON.NET.
JSON.NET is easier to use, better performance and has far more features.
http://www.newtonsoft.com/json/help/html/JsonNetVsDotNetSerializers.htm

you need to import System.Runtime.Serialization dll from reference

You need to add a reference to your project.
In the Solution Explorer right click references then add reference. You'll see a list of DLL's and you have to check the box next to the one you need for it to be added to the project. After you've done this you can successfully add the using statement.
Hope that helps!

The general process for serializing and deserializing JSON from C# is:
Add a reference to the System.Runtime.Serialization library.
Add using directives for System.Runtime.Serialization and System.Runtime.Serialization.Json.

Please change your Target framework from .NET Framework 4 Client Profile to .NET Framework 4

I know this is an old question, but I came across this in .NET 5.0 and the solution is to add using System.Text.Json to the top of your code.

Related

C# using System.Xaml missing assembly reference

I'm writing a C# Console Application that is targeted towards .Net 4.5. I want to use Xaml Services to save and read a List data structure to file. I'm using VS 2013 Pro.
The .net doc's say Xaml has been in .NET since 4.0(?) I have my projected targeted to 4.5, but even with 4.0, 4.5.1, 4.6, and 4.6.1... same missing assembly reference. I'm doing a
using System;
using System.Collections.Generic;
using System.IO;
using System.Xaml; // <-- this is getting the assembly error
using System.Xml;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
But that is where the missing reference is getting the error...
I've used it before in Win Forms. It is that maybe it's excluded for console applications? Or am I missing something that Xaml Class depends on before using that assembly?
Could it be that you did not add a project reference to the assembly "System.Xaml.dll"? The Xaml functionality is not contained in the assemblies which are included in a new VS console project by default. (Right-Click on the "References" entry of the project, then select "Add References", then browse to "Framework" and look for System.Xaml, which refers to the dll of that name).
A namespace however does not necessarily correspond uniquely to an assembly, so you might require even more assemblies. If you know which types you need, you can browse the MSDN documentation for looking which assembly might still be required.

HttpUtility not recognised in .Net 4.5

I Developed a WinForm application in with the target framework set to .net 4.0, now I wish to add to a project that has it's target framework set to .net 4.5. After I added the 4.0 WinForm application to my 4.5 project I keep getting the an error on my HttpUtility object.
data += "&batch_data=" + HttpUtility.UrlEncode(batch, System.Text.Encoding.GetEncoding("ISO-8859-1"));
"The name 'HttpUtility' does not exist in the current context"
I did include the System.Web namespace where the HttpUtility is located.
Visual Studio Error:
CS0234 The type or namespace name 'HttpUtility' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
The problem is somewhere else.
As you can see in MSDN the HttpUtility class is present in System.Web in .NET Framework 4.5.
You're probably targeting the Client Profile: target the full framework in Project Properties. Otherwise:
either you did not add the right using statement using System.Web;
or you did not add the reference to System.Web.dll in the project.
WebUtility
You also have another possibility: Use the WebUtility class.
The WebUtility class is recommended by Microsoft itself and should be used outside of web applications.
Like the HttpUtility class it also provides you with the possibility to encode and decode URLs.
This way you don't have the problems with importing the library into your project or setting some specific profiles.
From the Documentation (Source)
The HttpUtility class is used internally by the HttpServerUtility class, whose methods and properties are exposed through the intrinsic ASP.NET Server object. Additionally, the HttpUtility class contains encoding and decoding utility methods that are not accessible from the Server.
To encode or decode values outside of a web application, use the WebUtility class.
The HttpUtility class exists from .NET 1.1, so I think it is not possible for regular projects to 'not see it', as long as you have included a reference to System.Web.
You might be using a PCL (Portable Class Library), which uses a stripped down version of the framework that is supported on the platforms you selected, like Windows Store apps, Windows Phone, Silverlight, etc.
I hope this link will help you.
http://msdn.microsoft.com/en-us/library/system.web.httputility(v=vs.110).aspx
Dot net framework 4.5 support HttpUtility as it is under System.Web namespace.
Also adding a System.Web reference, without System.Web.Extensions reference into your project. If it doesn't work remove the existing and add new reference of System.Web into project. Also check which framework it is targeting it should be .NET Framework 4 or 4.5 without Client.
I encountered this issue in .net 4.5.2 (using VS2019). I did check that I was using full framework and I also tried explicitly declaring System.Web in a using statement, though VS complains that the using clause is not needed.
System.Web.Utility appears to have been replaced by System.Net.Webutility

Cannot find JavaScriptSerializer in .Net 4.0

I cannot seem to find the JavaScriptSerializer object nor the the System.Web.Script.Serialization namespace within Visual Studio 2010. I need to serialize something to JSON what am I supposed to use?
And yes, I already included the System.Web.Extensions (in System.Web.Extensions.dll) within the project. Which is why I am shocked?
I do know System.Web.Extensions was marked as obsolete in 3.5
Check if you included the .net 4 version of System.Web.Extensions - there's a 3.5 version as well, but I don't think that one works.
These steps work for me:
Create a new console application
Change the target to .net 4 instead of Client Profile
Add a reference to System.Web.Extensions (4.0)
Have access to JavaScriptSerializer in Program.cs now :-)
Right click References and do Add Reference, then from Assemblies->Framework select System.Web.Extensions.
Now you should be able to add the following to your class file:
using System.Web.Script.Serialization;
From the first search result on google:
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
JavaScriptSerializer Class
Provides serialization and deserialization functionality for AJAX-enabled applications.
Inheritance Hierarchy
System.Object
System.Web.Script.Serialization.JavaScriptSerializer
Namespace: System.Web.Script.Serialization
Assembly: System.Web.Extensions (in System.Web.Extensions.dll)
So, include System.Web.Extensions.dll as a reference.
I'm using Visual Studio 2015 and finally ran across this post.
Yes in order to use
JavaScriptSerializer json = new JavaScriptSerializer();
You must right click on references and under Assemblies --> Framework choose
System.Web.Extensions
Then add in your reference
using System.Web.Script.Serialization;
This is how to get JavaScriptSerializer available in your application, targetting .NET 4.0 (full)
using System.Web.Script.Serialization;
This should allow you to create a new JavaScriptSerializer object!
using System.Web.Script.Serialization;
is in assembly : System.Web.Extensions (System.Web.Extensions.dll)
Are you targeting the .NET 4 framework or the .NET 4 Client Profile?
If you're targeting the latter, you won't find that class. You also may be missing a reference, likely to an extensions dll.
For those who seem to be following the answers above but still have the problem (e.g., see the first comment on the poster's question):
You are probably working in a solution with many projects. The project you appear to be working in references other projects, but you are actually modifying a file from one of the other projects. For example:
project A references System.Web.Extensions
project A references project B
But if the file you are modifying to use System.Web.Script.Serialization is in project B, then you will need to add a reference to System.Web.Extension in project B as well.
Did you include a reference to System.Web.Extensions? If you click on your first link it says which assembly it's in.
You have to add the reference to the project.
In Assemblies, there is a System.Web.Extensions Add that.
Once that is done put:
using System.Web;
using System.Web.Script;
using System.Web.Script.Serialization;
That worked for me.
You can use another option which is the Newtonsoft.Json, you can install it from NuGet Package Manager.
Tools >> Nuget Package Manager >> Package Manager Console by issuing command
Install-Package Newtonsoft.Json
or
by using the GUI at Tools >> Nuget Package Manager >> Manage NuGet Packages for Solution...
Just so you know, I am using Visual Studio 2013 and have had the same problem until I used the Project Properties to switch to 3.5 framework and back to 4.5. This for some reason registered the .dll properly and I could use the System.Web.Extensions.

C# using System.Linq error

Why might "using System.Linq" cause the following error?
The type or namespace name 'Linq' does
not exist in the namespace 'System'
Reference System.Core
And then there are others that merge this namespace too - but that's the primary one on .Net 3.5 and above.
If you're project is currently .Net 2.0, say, and you're using the right version of VS (2005 and above) - you can simply right-click on the proejct properties; and change the 'Target Framework Version' to 3.5. System.Core will then become available.
If you don't see that in the options - then I guess you're using an older VS
The most probable reason is that you are using wrong version of .NET Framework.
Try to add System.Core assembly to your project
You'll get this error if you don't have "System.Core.dll" referenced (the assembly which contains the core LINQ APIs).
System.Linq is available in .Net 3.5 and above version.
Maybe you're targeting an older framework, Linq came in with 3.5 IIRC.
You are using lower version of .NET Framework than 3.5 to compile the source code or you don't have added the System.Core assembly to your project.
Manually type using System.Linq in the starting of the project, you will not be able to find this namespace in add reference dialogue box.
If you are still getting error then try to Add Reference System.Core.
If you are getting an error that it has been already referred then you can unload your project and then edit your csproject file, manually copy reference to System tag and paste and change the name to System.Core and reload the project.
In my case the only thing that worked was:
Adding a new Razor item (e.g. MVC 5 View Page)
That automatically pulls in some NuGet packages
The package that makes System.Linq available to Razor Views IntelliSense seems to be Microsoft.AspNet.WebPages.

System.Windows.Forms.DataVisualization Namespace Fine in One Class but Not in Another

I'm getting this error
The type or namespace name 'DataVisualization' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?)
Here is my using section of the class:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms.DataVisualization.Charting.Borders3D;
using System.Windows.Forms.DataVisualization.Charting.ChartTypes;
using System.Windows.Forms.DataVisualization.Charting.Data;
using System.Windows.Forms.DataVisualization.Charting.Formulas;
using System.Windows.Forms.DataVisualization.Charting.Utilities;
namespace myNamespace {
public class myClass {
// Usual class stuff
}
}
The thing is that I am using the same DataVisualization includes in another class. The only thing that I can think that is different is that the classes that are giving this missing namespace error are Solution Items rather than specific to a project. The projects reference them by link. Anyone have thoughts on what the problem is? I've installed the chart component, .Net 3.5 SP1, and the Chart Add-in for Visual Studio 2008.
UPDATE: I moved the items from Solution Items to be regular members of my project and I'm still seeing the same behavior.
UPDATE 2: Removing the items from the Solution Items and placing them under my project worked. Another project was still referencing the files which is why I didn't think it worked previously. I'm still curious, though, why I couldn't use the namespace when the classes were Solution Items but moving them underneath a project (with no modifications, mind you) instantly made them recognizable. :\
You are very likely missing a reference to the DataVisualization DLL. Note that although they share the namespace of System.Windows.Forms.dll, they aren't actually contained within it.
Solution items aren't used by compiled assemblies.
http://msdn.microsoft.com/en-us/library/1ee8zw5t.aspx
"They can be referenced by projects, but are never included in solution or project builds"
As far as I know, solution folders/items are really just meant for organizing things.
Are you getting actual build errors or just squiggles? Try building and look at the output window, does it succeed or fail?
In VS 2008 SP1 C# introduced a top level error squiggling feature. It's possible that if you open the solution item version of the file it will squiggle because of a lack of default references. The solution should still build correctly though.
If this is not the case try adding the file directly to the project (no link). See if that eliminates the error. If so then we know it has to due with a linked file and it can help track down the problem.

Categories

Resources