I want to save this object:
Student s = new Student();
to Json file. But Visual Studio 2012 can't find none of these namespaces:
System.Web.Script;
System.Json;
System.Runtime.Serialization.Json;
Any idea?
If you want to serialize class into Json you can try this one too:
Install JSON.NET if you haven't yet.
make sure to include using Newtonsoft.Json;
and you can try this code:
Student s = new Student();
string json = JsonConvert.SerializeObject(s);
sorry for my bad english.
Step 1: Google "System.Runtime.Serialization.Json Namespace" and find this page:
System.Runtime.Serialization.Json Namespace
Step 2: Click on the class you are interested in ( let's say JsonReaderWriterFactory Class )
Step 3: Read the part that says what assembly that class is in:
Assembly: System.Runtime.Serialization (in
System.Runtime.Serialization.dll)
Step 4: Add that DLL as a reference to your project. See: How to: Add or Remove References By Using the Add Reference Dialog Box
Step 5: Repeat Steps 1 - 4 as needed.
Make sure you add the appropriate references to your project. For example, to use the datacontractjsonserializer class, you need to add a reference to System.Runtime.Serialization to your project.
If you're using visual studio, read How to: Add or Remove References in Visual Studio.
You can then use it with:
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Student));
ser.WriteObject(outputStream, student);
Of course there are many other ways to do this and I won't go into detail about each one (I'm sure you've found some examples since you listed those namespaces).
TIP: if you want to use a .NET class and you're not sure what reference you need to add to your project in order to use it, open up the MSDN page for the class and look for the text like:
Assembly: System.Runtime.Serialization (in System.Runtime.Serialization.dll)
That's the assembly reference you need to add to your project.
The other answers are correct in saying that you need to add references to the appropriate assemblies. As an easy way to manage references for a personal project, I'd recommend NuGet, a package manager for Visual Studio. Thankfully, NuGet ships with Visual Studio 2012, so you just have to right-click the project, click on "Manage NuGet packages", and search for Json.NET. The first blog post I found on using NuGet in Visual Studio 2012 is here, and it seems to give a nice set of instructions, complete with screenshots.
You probably need to add references to your project:
System.Runtime.Serialization.Json is in System.Runtime.Serialization
System.Web.Script is in System.Web.Extensions
System.Json is only for Silverlight
Related
I am working on a project that will need to ability to download youtube videos. I found this project on github:
https://github.com/flagbug/YoutubeExtractor
My project already has a namespace. How would I import the YoutubeExtractor into my project? Do I need to change the namespace for it before (or after) importing it? Or is it up to me, in which case, what are the advantages and disadvantages to changing the namespace vs. not changing it? I am using VS Express 2012, if that matters.
My project already has a namespace. How would I import the YoutubeExtractor into my project?
You add reference to the external assembly (in this particular case you install the NuGet) and then add using statement with the correct namespace (YoutubeExtractor) in which the classes are defined.
So just follow the steps described on the home page:
Install-Package YoutubeExtractor
and then:
using YoutubeExtractor;
and finally:
// Our test youtube link
string link = "insert youtube link";
/*
* Get the available video formats.
* We'll work with them in the video and audio download examples.
*/
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);
Open the project that exists for the YouTubeExtractor and build it.
Move the outputted assembly to a location inside your project structure.
Add a reference to that assembly.
Add a using {namespace} to the files you want to use the extractor in.
Where {namespace} is the namespace it uses.
Further, it appears that there is a nuget package for it (you see that YoutubeExtractor.nuspec file in root). I would recommend installing nuget into Visual Studio and then searching nuget for the YouTubeExtractor. It's a lot easier, and you get updates with it easier as well.
I have been trying to include DirectX in C sharp project (Visual Studio 2010).
I installed DirectX SDK and included the components as:
using Microsoft.DirectX;
using Microsoft.DirextX.Direct3D;
When I try to compile, I get the error: the type or namespace name 'directx' does not exist in the namespace 'microsoft'
Some blogs mentioned that I need to add 'reference' - Microsoft.DirectX under '.NET' tab. But I couldn't find it over there. Neither was any facility to add it to the tab.
Any suggestion will be highly appreciated.
This was deprecated a long time ago. The last Direct SDK that still has the managed wrappers is February 2010. The download is available here. You'll get to pick the references you are looking for after you install that one.
Better not to use it, no future, look at something like the open source SlimDX or SharpDX projects.
There are various reasons why a dll would not appear on the .Net tab.There is specific registry configuration that makes certain dlls to appear on the .Net tab.
Alternatively use the browse option to add the dll reference manually to your project.
Project >> Add Reference
This is the way to add reference for Directx in C# Program :
Go to the solution explorer
Click references
Click add reference
Click Browse
Go to : "C:\WINDOWS\Microsoft.NET\DirectX for Managed Code\1.0.2902.0\"
*usually this is the place of Directx Files located. This can be change with the installation of .net.
Select your DirectX package.
example :
(C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0\Microsoft.DirectX.dll)
Here is one way i have find. First you need to install DXSDK (i have June2010 and SDK for Windows 7). Go to "Add refference" dialog press "Browse" , go to something like this "C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0" or "C:\Windows\Microsoft.NET\Managed DirectX\v9.02.2904" then choose proper dll for example - Microsoft.DirectX.dll. So you can add "using Microsoft.DirectX" directive to your project.
I'm trying to use DataAnnotations in my WPF project to specify a maximum length of strings, with the following:
using System.ComponentModel.DataAnnotations;
However, I get the error
The type or namespace name 'DataAnnotations' does not exist in the
namespace 'System.ComponentModel' (are you missing an assembly
reference?)
I've seen other examples where DataAnnotations does exist in this namespace. I'm using C#4. Is there any reason why I can't use this? What can I do to fix it?
You have to reference the assembly in which this namespace is defined (it is not referenced by default in the visual studio templates). Open your reference manager and add a reference to the System.ComponentModel.DataAnnotations assembly (Solution explorer -> Add reference -> Select .Net tab -> select System.ComponentModel.DataAnnotations from the list)
If using .NET Core or .NET Standard
use:
Manage NuGet Packages..
instead of:
Add Reference...
To Reference System.ComponentModel.DataAnnotations
In a code file to have Using System.ComponentModel.DataAnnotations; at the top of the file such as:
using System.ComponentModel.DataAnnotations;
Add a .NET reference to your project by right clicking the project in solution explorer:
Hope this helps! This question helped me.
If you don't have it in references (like I did not) you can also add the NuGet System.ComponentModel.Annotations to get the assemblies and resolve the errors. (Adding it here as this answer still top of Google for the error)
I also had the same problem and I resolved by adding the reference in one of my projects which didn't had the mentioned reference. If you have 2-3 projects in your solution, then check by adding this reference to the other projects.
I found that I cannot reference System.ComponentModel.DataAnnotations from Silverlight 5 with the below version at (1). I found that Silverlight 5 assemblies cannot use .NET assemblies, it gives the error "You can't add a reference to System.ComponentModel.DataAnnotations as it was not built against the Silverlight runtime. ..." I plan to workaround this by hopefully installing the Silverlight 5 package found at (2) below. If this fails I will update this post.
[UPDATE: it failed. I installed everything relating to Silverlight 5 and I don't have the Silverlight version of the .dll assembly System.ComponentModel.DataAnnotations . Too bad. UPDATE II: I found an old .dll having this name from a previous installation of Silverlight developer's kit for Visual Studio 2008 or 2010. I added this file and it seems to 'work', in that IntelliSense is now recognizing attributes on class members, such as [Display(Name = "My Property Name")]. Whether or not this works for everything else in this .dll I don't know.]
(1)
Microsoft Visual Studio Professional 2013
Version 12.0.21005.1 REL
Microsoft .NET Framework
Version 4.5.51641
Installed Version: Professional
(2)
http://go.microsoft.com/fwlink/?LinkId=229318
I searched for help on this topic as I came across the same issue.
Although the following may not be the Answer to the question asked originally in 2012 it may be a solution for those who come across this thread.
A way to solve this is to check where your project is within the solution. It turns out for my instance (I was trying to install a NuGet package but it wouldn't and the listed error came up) that my project file was not included within the solution directory although showing in the solution explorer. I deleted the project from the directory out of scope and re-added the project but this time within the correct location.
Use the FrameWork version 4.5 and above for your project then problem solved.Because this namespace is under 4.5 and above.
System.ComponentModel.DataAnnotations is contained in its own assembly so you need to make sure you have it refernced. Just simply:
1). Right click on Soloution and choose add.
2). Choose reference from the list.
3). Search " System.ComponentModel.DataAnnotation " and tick the check box on its left hand side and press ok.
Job done, shouldnt have any refernce errors.
If you tried to update visual studio from vs2008 to vs2010. And your app uses framework 3.5 (and you don't want to upgrade it), and also used WCF RIA Services BETA... I have bad news... you MUST upgrade to WCF RIA Services v1 (BETA does not work on vs2010)... and due to this... you also have to install Silverlight 4 + upgrade to framework 4.0
See this:
http://blog.nappisite.com/2010/05/updating-visual-studio-2008net-35-ria.html
I upgraded from Silverlight 4 to Silverlight 5 and then I was having this issue. Although I had a reference to "System.ComponentModel.DataAnnotations" under "References" in my project, it had a yellow yield sign by it that indicated the previously referenced assembly could not be found. It turned out that the properties of the "System.ComponentModel.DataAnnotations" reference indicated "Specific Version = True", when I changed this to "Specific Version = False" it fixed the issue. Right click on the "System.ComponentModel.DataAnnotations" assembly under "References" and select "Properties" from the context menu. Check that the property value for "Specific Version = False".
It must have been referencing the old Silverlight 4 assembly which was no longer available after the upgrade to Silverlight 5.
I also have this problem.
That is very stupid when i add a namespace the same with System. I try to remove all references, but it is not resolved. I use "global::System.ComponentModel", it is working as well.
When i remove my namespace, this problem has been resolved.
For .Net Core in Visual Studio 2019 try this.
see VS suggestion
It worked for me, hope it'll work for you as well.
I was moving from .Net Framework 4.7.2 to .Net Standard 2.0.
In my case, I had to change DataAnnotations's reference from an Assembly reference to a Nuget package.
This error occurs when the reference to the "System.dll" got removed.Solution to the problem is very simple add the reference to "System.dll".The dll is normally available in the following location
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727" .Add the reference your problem will get solved .
There was a problem using System.ComponentModel.DataAnnotation in net40 so I just did:
#if !NET40
using System.ComponentModel.DataAnnotations;
#endif
#if !NET40
[StringLength(256)]
#endif
The NET40 must be a predefined macro definition for .Net Framework 4.0
I had same problem, I solved this problem by following way.
Right click on page, select Property. in build action select Content.
Hope that this solution may help you.
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.
I have a string that I'd like to encode into the standard URL format. From what I've found, I should be able to do this via the httpUtility.urlEncode method, but I don't seem to have that available.
I've added "using" references to both System.Web and System.Net to no avail. I've also seen other references to server.urlEncode amongst other variants, but I don't see the method anywhere.
I'm using the latest version of C# in Visual Studio 2010. Is the method called something different in this version, hidden somewhere else, or am I completely off base?
By default, new projects in Visual Studio 2010 target the .NET Framework 4.0 Client Profile, which does not include the System.Web assembly.
You can change the version of the Framework that your project targets in your project's Properties. Under the "Application" tab, select ".NET Framework 4.0" from the combobox labeled "Target framework".
Then, make sure that you have added a reference to System.Web using the "Add Reference" dialog.
Finally, add a using directive to the top of your class for the System.Web namespace:
using System.Web;
You'll find the various overloads of the UrlEncode method in the HttpUtility class. Sample code:
HttpUtility.UrlEncode("http://www.google.com/");
In .Net 4.5 you can (should?, 'please use' says a Katana comment) use the System.Net.WebUtility.UrlEncode method.
It can't be named differently since Visual Studio doesn't supply the class or method names, the .NET framework does.
All I can tell you is that the System.Web.HttpUtility AND System.Web.HttpServerUtility classes contain a method called UrlEncode(string).
If your project target ".NET Framework X Client Profile",you cannot not use "System.Web",but you can use "Uri.EscapeUriString | Uri.UnEscapeUriString" instead.
Yes, adding the reference was my answer. But be sure you double check the project, that it is in, if you have more than 1 project in your solution. I had a solution with 3 projects. System.Web was added to 2 projects but not the 3rd project.
I spent an hour trying to figure out why I couldn't use HttpUtility since it was a Reference in the main project. But I didn't check the sub-projects of the Solution.
Hope it helps someone.
Because you only see AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevel, I strongly suspect (like the other guys) that you're missing a reference.
The best you can do is start a new project, because it's pretty complicated to add/remove references without ruining your entire project.
How to: Add or Remove References in Visual Studio (MSDN) shows how to add/remove references. In your case, you should check/add the System.Web reference.