0

This may be a stupid question but how can I add XML references in Visual Studio to a single c# file?

I want to use using System.xml;, but Visual Studio is not able to find it. After a little research, I found out that I have to reference the DLL. But I only created a single C# script and no project: the project window on the right side is empty (shows 0 projects) and, when I right-click on it, there is no option for adding a reference.

The script should provide a few functions for reading specific XML files and basically should be a plug-in which can be implemented in any C# program if needed - so I think I don't really want to make an application out of it.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
San
  • 1
  • Right click project, manage nuget packages, install package System.Xml – Jawad Dec 31 '19 at 16:35
  • If there is no project loaded, you need to create a new one first. Then you can add references to that. How are you running this C# script? PowerShell? CSH? – Ron Beyer Dec 31 '19 at 16:36
  • @RonBeyer dindt rlly tought about that tbh. i think i never created a single c# file. always a console project, wpf project or use vs as my ide for unity. the plan was to drop the c# file into my unity project and use the function it provides. but it also should function as a plugin for any other programm based on c#. i thought creating a c# file and refernce it in the 'final application' should do the trick. how do it best get started with the plugin if i really need a project? – San Dec 31 '19 at 16:49
  • You *can* use it that way, but the project you are including it in must have a reference to the Xml namespace for it to work. The best way I can think of to test this is to create a test project and then add the file (right click the project, select Add Existing File, then instead of just selecting the file drop down the arrow on the button and select "add as link"). Then you can test it as you would calling it from any other project. If you want to include it as an assembly (DLL) you should start with a library project and put your plugin code in that. – Ron Beyer Dec 31 '19 at 16:51
  • @RonBeyer okay i will try the second method. i definitely want the xml reference in the plugin. thanks! – San Dec 31 '19 at 17:01
  • From Solution Explorer : Right Click Namespace : Add : Add Existing Object : Select \*.\* and browse toward xml file. Then Right Click XML File : Select Properties : Copy To Output Directory. – jdweng Dec 31 '19 at 20:35

1 Answers1

0

You can use the following code to add xml reference to c# script and call the script successfully.

 static void Main(string[] args)
    {
        string code = File.ReadAllText("D:\\Code.cs");
        CSScript.Evaluator.ReferenceAssembliesFromCode(code);
        dynamic block = CSScript.Evaluator.LoadCode(code);
        block.ExecuteAFunction();

        Console.ReadKey();
    }

Code.cs

using System;
using System.Xml;
 class MyScript
    {
        public void ExecuteAFunction()
        {
            string path = "D:\\t.xml";
            XmlDocument document = new XmlDocument();
            document.Load(path);
            Console.WriteLine(document.LastChild.InnerXml);
            Console.ReadKey();


        }
    }

Besides, you also look at How to Add a Reference to a C# Script.

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27