System.Xml in visual studio not working - c#

I am trying to process XML with C# in Visual Studio and it will not allow me to import System.Xml. I looked at other posts about this problem and this was usually caused by misspelling System.Xml to System.XML. I have the right spelling so I am not sure what is causing me problems. I looked under references and there is no System.Xml and I am using Visual Studio 2013 The error message was
Error 1 The type or namespace name 'Xml' does not exist in the namespace 'System' (are you missing an assembly reference?)
Do I have to download System.Xml?
Here is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace WebTesting
{
class Test1
{
public static void Main(string [] args)
{
using(XmlReader reader = XmlReader.Create("myData.Xml"))
{
while(reader.Reader())
{
if(reader.IsStartElement())
{
Console.Write("The start element is " + reader.ReadString());
}
}
}
Console.ReadLine();
}
}
}

You'll need to add an assembly reference. Right-click "References" under your project in Solution Explorer and select "Add Reference..." Switch to the .NET tab and find System.Xml. Click OK.

by using XDocument (in using System.Xml.Linq;) you'll get more flexible functionality to handling XML-documents

Related

The namespace name 'SyntaxTree' could not be found

I have am developing in Unity 2019.2.3f1. I am trying to write a script that can Customize project files created by VSTU. In case the link ever gets removed, I have included a script very similar to the one from the link.
#if ENABLE_VSTU
using System.IO;
using System.Text;
using System.Xml.Linq;
using UnityEditor;
using SyntaxTree.VisualStudio.Unity.Bridge;
[InitializeOnLoad]
public class ProjectFileHook
{
private class Utf8StringWriter : StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
}
static ProjectFileHook()
{
ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) =>
{
// parse the document and make some changes
XDocument document = XDocument.Parse(content);
document.Root?.Add(new XComment("FIX ME"));
// save the changes using the Utf8StringWriter
Utf8StringWriter str = new Utf8StringWriter();
document.Save(str);
return str.ToString();
};
}
}
#endif
The issue is, using SyntaxTree.VisualStudio.Unity.Bridge; fails to compile due to the error error CS0246: The type or namespace name 'SyntaxTree' could not be found (are you missing a using directive or an assembly reference?).
I have checked both Unity and Visual Studio that the Visual Studio Tools for Unity are installed and enabled.
Why is the code failing to compile? Am I missing something that is preventing it from compiling?
Just incase anyone else has this issue.
This issue was being caused by the scripts position. The script has to be under the Editor folder, otherwise the script won't work as expected.
Credits to therealjohn for pointing it out here.

Convert LinqPad C# Program to Visual Studio C# Console Program

This seems obvious but I cannot see an easy way to to do this.
I have written a C# Program in LinqPad and it runs well, is there a guide for moving this to a Visual Studio Console Program.
A simple program such as this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataContext dataContext = this;
var products = dataContext.GetTable<Product>();
var query = from p in products
select p;
query.Dump();
}
}
}
produces these errors:
Error 1 The type or namespace name 'DataContext' could not be found (are you missing a using directive or an assembly reference?) c:\users\xxx\documents\visual studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs
Error 2 Keyword 'this' is not valid in a static property, static method, or static field initializer c:\users\xxx\documents\visual studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs
Error 3 The type or namespace name 'Product' could not be found (are you missing a using directive or an assembly reference?) c:\users\xxx\documents\visual studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 14 49 ConsoleApplication1
I must have to add some references, add a connection to the database, I just need a guide for what to add to my project but I cannot find one.

Error Id: CS0234, Error: The type or namespace name 'Xml' does not exist in the namespace 'System.Security.Cryptography'

I am using an online IDE (http://www.frosthawk.net/) to run my C# code and upon compilation I get the error
Error Id: CS0234, Error: The type or namespace name 'Xml' does not exist in the namespace 'System.Security.Cryptography' (are you missing an assembly reference?), Line: 26, Column: 36
Can anybody tell me how to add this reference to my code whicle using the the online IDE as I cannot right click and Add Reference that we used to do normally
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;
public class SignVerifyEnvelope
{
public static void Main(String[] args)
{
try
{
......
Perhaps you missed this:
// IMPORTANT How-To-Use:
// # Add/Manage References by clicking the 'References' link
// # Manage Your Build Settings by clicking the 'Settings' link
Click references and fix your reference.
If you are unsure which reference you require, you may need to browse MSDN for help. For reference here is the System.Security namespace.

CS0234 - Cannot Instantiate C# class in ASP.NET

I made the same ASP.NET C# project in both VS2010 and MonoDevelop using these two classes among the standard files (Site.Master, Web.Config, Default.aspx, etc.) and recieve this same error (CS0234) seen at the bottom.
Login.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using mynamespace;
namespace mynamespace
{
public partial class Logon
{
public void btnClicked(object sender, EventArgs e)
{
//ERROR IS HERE:
mynamespace.Test session = new mynamespace.Test();
//Obviously, this doesn't work either:
Response.Write(session.echoUser());
}
}
}
Test.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mynamespace;
namespace mynamespace
{
public class Test
{
public string echoUser()
{
return "foobar";
}
}
}
I recieve the same error in both IDEs, here is the MonoDevelop error:
The type or namespace 'Test' does not exist in the namespace 'mynamespace' (are you missing an assembly reference?) (CS0234) Logon.cs
Basically, the class Test refuses to instantiate. Any input is appreciated!
If you have that Test class in an ASP.Net web project, then you need to place it in the App_Code folder, not just anywhere in the site.
You need to refer the projects to each other, if you haven't done so yet. I'm guessing that you don't get the intellisense to show the class in the other namespace, right?
You can see the References on the right side (most cases) under your project view. You can right click there and choose to Add reference. Then you browse to the binaries from the pther projects. (You might be able to point to the project itself too - I don't have VS in front of me at the moment.)
Also, It's a convention to use camel case for namespaces, so it should be MyNameSpace.
If the classes are in the same project, you might want to skip using mynamespace and refer to the class by Test intead of mynamespace.Test.
If these are both in the same project, please check that both files are included in the project and have the "Build Action" property set to "Compile". Please also check that all of these namespaces have exactly matching spelling and casing.

How to Use XDocument class in SilverLight Project (C#)

I'm trying to create a Silverlight application (for the first time) that involves parsing XML from a site and displaying information. To do this I am using Visual Studio 2008 on Windows XP Service Pack 3. I also have .NET Framework 3.5 SP1 installed.
My problem is that no XML-parser I have seen on the internet works. The top of my code I have both lines I believe are necessary (using "System.xml;" and using "System.linq;") but XDocument, XMLReader, XMLDocument, and any others I have found do not work, returning the error that the type or namespace cannot be found. I do have .NET Framework.
I have turned absolutely nothing up on the internet regarding this problem. Does anyone have any ideas?
EDIT: I just discovered that when I open the file outside of the context of a Silverlight project, it is able to use XDocument. It is only when I open the entire project that my problem occurs
Here is some sample code showing the problem:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq; //Error 1 (See below)
namespace LastfmAmazon
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
public void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XDocument doc = XDocument.Parse(e.Result); //Error 2: see below
}
public void Button_Click(object sender, RoutedEventArgs e)
{
if (uname.Text != String.Empty)
{
App app = (App)Application.Current;
app.UserName = uname.Text;
String getTopArtists = "http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=" + app.UserName + "&api_key=d2d620af554a60f228faed8d502c4936";
uname.Text = "Try Another One!";
WebClient web = new WebClient();
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompleted);
client.DownloadStringAsync(new Uri(getTopArtists));
}
}
}
}
Error 1: This line contains the following error: The type or namespace name 'Linq' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?)
Error 2: This line contains the following error: The type or namespace name 'XDocument' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?)
EDIT 2: Once I Googled what it meant to "add a reference" to a library, Anthony's answer solved the problem.
By default a Silverlight project will contain the System.Xml dll however XDcoument is contained in the System.Xml.Linq dll, this you will have to add to your project.
Make sure you add a reference to the appropriate XML library
For XMLDocument, XMLReader, etc ...: System.Xml.Dll
For XDocument, XNode, etc ...: System.Xml.Linq.dll

Categories