Dblock247
Dblock247

Reputation: 6725

Building XML Response in ASP.NET MVC 6

I have a need to Build up and xml string and return it through an api action on one of my MVC 6 controllers. I have learned to build up an xml document using Linq-to-xml (XDocument) api. However in mvc Linq-to-xml Doesn't seem to be available as it doesnt build on the dnx core 5.0. Is there another way to build up an xml document which is works with the new Technology?

EDIT: I have an MVC 6 project and a Class Library. I am trying to use Linq-to-Xml in the class library. I need to return xml for a request that Freeswitch will make. I has a xml file format that changes slightly so i need to be able to construct the xml as needed.

The Easiest way that i have seen so far is to construct a file with Linq-to-Xml. I was thinking i could convert that to a string and return in as xml for an ajax request. If there is a better way im all ears.

MVC 6 project.json:

{
   "userSecretsId": "aspnet5-Freeswitch-8e336a2b-a401-4eec-979e-1da4917e7d74",
      "version": "1.0.0-*",
      "compilationOptions": {
        "emitEntryPoint": true
      },

      "dependencies": {
        "Bestro.Repositories": "1.0.0-*",
        "Bestro.Services": "1.0.0-*",
        "EntityFramework.Commands": "7.0.0-rc1-final",
        "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
        "EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
        "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
        "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
        "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
        "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
        "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
        "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
        "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
        "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
        "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
        "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
        "Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
        "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
        "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
        "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
        "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
        "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
        "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
            "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
            "System.Xml.XDocument": "4.0.0-beta-22231"
      },

      "commands": {
        "web": "Microsoft.AspNet.Server.Kestrel",
        "ef": "EntityFramework.Commands"
      },

      "frameworks": {
            "dnx451": {
                "frameworkAssemblies": {
                }
            },
        "dnxcore50": {
          "dependencies": {
          }
        }
      },

      "exclude": [
        "wwwroot",
        "node_modules"
      ],
      "publishExclude": [
        "**.user",
        "**.vspscc"
      ],
      "scripts": {
        "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ]
  }

and my Class Libraray project.json:

{
  "version": "1.0.0-*",
  "description": "Bestro.Services Class Library",
  "authors": [ "Londre Blocker" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",
  "frameworks": {
    "net451": { },
    "dotnet5.4": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Runtime": "4.0.21-beta-23516",
        "System.Threading": "4.0.11-beta-23516"
      }
    }
  },
  "dependencies": {
    "System.Xml.XDocument": "4.0.11-rc2-23623"
  }
}

The error im getting when i try to run the project:

Error CS0234 The type or namespace name 'Xml' does not exist in the namespace 'System' (are you missing an assembly reference?) Bestro.Services..NET Framework 4.5.1 Z:\Documents\Visual Studio 2015\Projects\Freeswitch\src\Bestro.Services\FreeswitchService.cs 5 Active

and:

Error CS0246 The type or namespace name 'XElement' could not be found (are you missing a using directive or an assembly reference?) Bestro.Services..NET Framework 4.5.1 Z:\Documents\Visual Studio 2015\Projects\Freeswitch\src\Bestro.Services\FreeswitchService.cs 22 Active

and:

Error CS0246 The type or namespace name 'XAttribute' could not be found (are you missing a using directive or an assembly reference?) Bestro.Services..NET Framework 4.5.1 Z:\Documents\Visual Studio 2015\Projects\Freeswitch\src\Bestro.Services\FreeswitchService.cs 131 Active

and:

Error NU1002 The dependency System.Xml.XDocument 4.0.0-beta-22231 in project Freeswitch does not support framework DNXCore,Version=v5.0. Freeswitch Z:\Documents\Visual Studio 2015\Projects\Freeswitch\src\Freeswitch\project.json 1

and:

Error NU1002 The dependency System.Xml.XDocument 4.0.0-beta-22231 in project Freeswitch does not support framework DNX,Version=v4.5.1. Freeswitch Z:\Documents\Visual Studio 2015\Projects\Freeswitch\src\Freeswitch\project.json 31

Upvotes: 1

Views: 990

Answers (1)

Pawel
Pawel

Reputation: 31610

Linq to Xml is availabe. You need to add a reference to "System.Xml.XDocument": "4.0.11-*" to your project.json and use the System.Xml.Linq namespace. This small tool uses Linq to Xml and is only Core Clr.

(As a side note - if you just need to write Xml using just XmlWriter will be more performant.)

Upvotes: 2

Related Questions