VSCode dependency for XDocument

I am trying to write a C# console app to read XML and process using XDocument. It seems straightforward, but I cannot get the intellisense to recognize and the compiler to build the code. I am obviously missing a reference, but don't see which.

Program.cs:

using System;
using System.Xml.XDocument;
using System.IO;

namespace TestVSCode
{
    public class Program
    {
        public static void Main(string[] args)
        {
             XDocument _xml = XDocument.Load(File.ReadAllText(@"C:\Temp\Test.xml"));
             Console.Read();
        }
    }
} 

The project.json file:

{
  "version": "1.0.0-*",
  "description": "TestVSCode Console Application",
  "authors": [ "" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",
  "tooling": {
   "defaultNamespace": "TestVSCode"
 },

 "dependencies": {
 },

  "commands": {
    "TestVSCode": "TestVSCode"
  },

  "frameworks": {
   "dnx451": { },
   "dnxcore50": {
     "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Console": "4.0.0-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Threading": "4.0.11-beta-23516",
        "System.Xml.XDocument": "4.0.11-beta-23516",
        "System.IO": "4.0.11-beta-23516",
        "System.IO.FileSystem": "4.0.1-beta-23516"
      }
    }
  }
}

Running dnu restore works fine, but dnu build gives this error:

C:\Temp\TestVSCode\Program.cs(2,14): DNX,Version=v4.5.1 error CS0234: The type or namespace name 'Xml' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\Temp\TestVSCode\Program.cs(11,12): DNX,Version=v4.5.1 error CS0246: The type or namespace name 'XDocument' could not be found (are you missing a using directive or an assembly reference?) C:\Temp\TestVSCode\Program.cs(11,29): DNX,Version=v4.5.1 error CS0103: The name 'XDocument' does not exist in the curren t context

Please help me understand what mistake I am doing.

Best regards, Vemund Haga

Jon Skeet
people
quotationmark

There are two problems here:

  • Your using directive tries to pull in a namespace ending in XDocument. You want using System.Xml.Linq; which is the namespace containing XDocument
  • You need to specify the framework assemblies you need for the XML classes for the DNX451 build. I haven't yet worked out exactly which framework assemblies are pulled in by default vs when you need to specify them, but in this case the following set works:

    "dnx451": {
      "frameworkAssemblies": {
        "System.Xml": "",
        "System.Xml.Linq": ""
      }
    }
    

See my somewhat related question to explain why frameworkAssemblies is required rather than just dependencies.

people

See more on this question at Stackoverflow