1

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

2
  • Do you mean that you want to return back common XML file or or have another case: you have Web API method in MVC6 application which returns JSON results and you need to return the data in some way in XML format? In other word in the first case you have already XML data and you want just return there and in the second part you have object (list of objects) and you need to return the results in XML form. Commented Jan 20, 2016 at 17:33
  • By the way you can use "System.Xml" and "System.Runtime.Serialization" include "System.Runtime.Serialization.Xml": "4.1.0-*", "System.Xml.XmlSerializer": "4.0.11-*" for core 5.0. You can use using System.Xml;` to access XmlWriter.Create method and some other which you need for writing XML data. Commented Jan 20, 2016 at 17:36

1 Answer 1

2

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.)

Sign up to request clarification or add additional context in comments.

6 Comments

I have this in a class library and for some reason i cant get it configured correctly. Can you explain further.
You need to provide more details - "doesn't build" or "can't get it configured" is too vague to tell what's going on. What is the error you get when compiling? Did you run dnu restore on your project?
sorry i was in the process when i saw your comment. Please see above.
In one project.json you use "dnxcore50" and in the other you use "dotnet5.4" - can it be the problem? Have you tried unify them?
I am not very good with the probject.json files. However this was the default settings when i added the project. and it ran perfectly until i needed to use XDocument.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.