What assembly does XmlDocument live in, and how do I use it? - c#

I have the following code:
using System.Xml;
namespace MyApp {
public class Foo : IBar {
public void SomeCallback(object sender, CallbackEventArgs e) {
string s = e.Result;
System.Xml.XmlDocument xml; // <-- Error CS0234.
...
This results in error CS0234: The type or namespace name 'XmlDocument' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?)
I have verified that System.Xml is included under "References" in Solution Explorer. It's listed as using 2.0.x.x, but that should be fine since the class allegedly existed then?
I'm not well versed in .net development, so I'm not sure how to troubleshoot this.

Based on the comment you posted to your question it looks like you are building a Silverlight application. The type XmlDocument is not included in the Silverlight version of the framework. The recommended alternative is XDocument
Here is a tutorial page on using XML in a Silverlight application
http://msdn.microsoft.com/en-us/library/cc189074(VS.95).aspx

Related

What assembly is SerializeableAttribute in?

I'm trying to set up serialization for a class. When I added:
[Serializeable]
I got the error:
"The type or namespace name could not be found (are you missing a using directive or an assembly reference?)"
I added System.Runtime.Serialization.Formatters.Soap and System.Runtime.Serialization but they didn't work. I also tried to find the other assemblies listed on the documentation page. I only found mscorlib in the VS Reference Manager and it was already added.
I wrote the code like so:
[Serializeable]
public class Test { }
Edit: I also tried:
[Serializeable()]
public class Test { }
I expected the code to compile without errors.
Edit: fixed wrong name for an assembly
Per MSDN, the SerializableAttribute type can be found in the following assemblies:
System.Runtime.Serialization.Formatters.dll
mscorlib.dll
netstandard.dll
System.Runtime.dll
EDIT
It may be easier to try the below. There may be namespace conflicts.
[System.SerializableAttribute]
public class Test {}

USQL - Custom Outputter failed to find NewtonSoft

I have a USQL Job which reads json from Azure Blob and after some data manipulation writes a single line JSON file to ADLS.
I have written a Custom Ouputter to write JSON File.
This is how my CustomOutputter files look like:
using Microsoft.Analytics.Interfaces;
using Microsoft.Analytics.Types.Sql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace demo{
[SqlUserDefinedOutputter(AtomicFileProcessing = true)]
public class JSONOutputter : IOutputter
{
//Actual Code Here
public static class Factory
{
public static JSONOutputter JSONOutputter(bool isHeader = true, Encoding encoding = null)
{
return new JSONOutputter(isHeader, encoding);
}
}
}
I am using this Ouputter in my USQL Job like this:
OUTPUT #final_output
TO "/output/json/final.json"
USING demo.Factory.JSONOutputter(isHeader: true);
When I compile my USQL Script using
ADL: Compile Script
I get this error:
[Info] Start compiling code behind: /users/kumar.pratik/documents/usql/second.usql.cs ...
[Error] Command failed: mono /Users/kumar.pratik/.vscode/extensions/usqlextpublisher.usql-vscode-ext-0.2.11/compilehost/compilehost.exe /users/kumar.pratik/documents/usql/second.usql.cs /Users/kumar.pratik/Documents/usql/usqlCodeBehindReference /Users/kumar.pratik/Documents/usql/second.usql.dll __codeBehind__tRYGlFBeSWcl
[Error] Compile failed
error: "The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)" at line: 8, column 6
error: "The type or namespace name 'JsonTextWriter' could not be found (are you missing a using directive or an assembly reference?)" at line: 17, column 12
error: "The type or namespace name 'JsonTextWriter' could not be found (are you missing a using directive or an assembly reference?)" at line: 44, column 34
Could someone please let me know how can I get this fixed?
I am working on Visual Studio Code on Mac OS
Thanks
Can you please share how your script looks like? In particular, are you
Using Visual Studio's code behind for your Outputter code?
If the answer to 1 is no: Did you register your Outputter and Newtonsoft lib in the U-SQL catalog with CREATE ASSEMBLY or via the Visual Studio Assembly registration feature? And did you reference the U-SQL assemblies (both your own code and Newtonsoft) in your script with REFERENCE ASSEMBLY?
More details: https://blogs.msdn.microsoft.com/azuredatalake/2016/08/26/how-to-register-u-sql-assemblies-in-your-u-sql-catalog/

Visual Studio adding prefix to stated namespace

I'm getting a few:
'The type or namespace 'blah' does not exist in 'A.B.C' (are you missing an assembly reference?)
The code looks like:
namespace A
{
using B.C;
public class Blah()
{
//...
}
}
A and B are both assemblies that are included in the solution. Both use .NET 4.0. Assembly B does in fact have a namespace C. Assembly B is included as a reference in assembly A.
Why is it looking for A.B.C when the reference is B.C?
The problem was a typo in a different file than the ones with errors.
In another file I had:
namespace A.B.C
{
//...
}
I must have done an accidental Ctrl-P at some point?
The steps I took to find the offending file:
Open up the most recent file that I had changed
Examine the namespace
Repeat with the next most recent

using namespace in C# from CLI/C++ class

This might be a C#-noob question...
Assume I have the following CLI/C++ header:
namespace DotNet {
namespace V1 {
namespace X {
public ref class AClass {
public:
AClass() {}
void foo() {}
static void bar() {}
};
}
}
}
Then from C# I do the following:
using DotNet;
V1.X.AClass a = new V1.X.AClass();
I get:
Program.cs(18,7): error CS0246: The type or namespace name 'V1' could
not be found (are you missing a using directive or an assembly
reference?)
Same with:
using DotNet.V1;
X.AClass a = new X.AClass();
Program.cs(18,7): error CS0246: The type or namespace name 'X' could
not be found (are you missing a using directive or an assembly
reference?)
What works is:
DotNet.V1.X.AClass a = new DotNet.V1.X.AClass();
Or
using DotNet.V1.X;
AClass a = new AClass();
So either I have to use the full namespace path, or I have to open all of the path to access the class. Is there anything I can do about this?
I would like to be able to open only a part of it.
So either I have to use the full namespace path, or I have to open all of the path to access the class.
Yes.
Is there anything I can do about this?
Not that I'm aware of. Why would you not just want a using directive for the whole namespace? Isn't that the simplest approach?
Note that this has nothing to do with C++/CLI. The same is true whatever the source language is - so you can see it with C# as well:
namespace DotNet.V1.X
{
public class AClass {}
}
As Jon Skeet stated you have to include the full namespace in either the using statement or each time you reference it in code. I recommend just placing the full namespace in the using statement.
You can, however, achieve that syntax with a using alias, but it does not add anything of value outside of syntax.
using V1 = DotNet.V1;
...
V1.X.AClass a = new V1.X.AClass();
Also if you are using C# version 3.0 or higher the var keyword will only require you type out the full namespace on the right side of the assignment.
var a = new DotNet.V1.X.AClass();

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