Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将xml内容下载到文件中_C#_Xml_Asp.net Mvc 3 - Fatal编程技术网

C# 如何将xml内容下载到文件中

C# 如何将xml内容下载到文件中,c#,xml,asp.net-mvc-3,C#,Xml,Asp.net Mvc 3,我想下载一个xml文件,但我有一个字符串中的xml内容 public FileResult Download(string id)//guid { string fid = Convert.ToString(id); var model = service.GetAllDefinitions().First(x => x.ID == id); var definitionDetails = new Stat

我想下载一个xml文件,但我有一个字符串中的xml内容

 public FileResult Download(string id)//guid
    {
        string fid = Convert.ToString(id);

        var model = service.GetAllDefinitions().First(x => x.ID == id);            
        var definitionDetails = new StatisticDefinitionModel(model);
        var definition = definitionDetails.ToXml;
         //in this definition i have xml content not a path
        string fileName = definitionDetails.Name + ".xml";
        string contentType = "text/xml";

        return File(definition , contentType);
    } 
但这不起作用,出现了类似非法路径的错误

谢谢,编辑 只有第一个选项有效。另外,请为客户端指定默认文件名:

 return File(Encoding.UTF8.GetBytes(definition), contentType, "somefilename.xml");

您应该会收到警告,因为您没有在函数中使用
fileName
。也许您可以从这里开始工作。所以您有一个字符串中的XML结构,并希望将其放入文件中并返回它?为什么不直接返回XmlDocument?@Gorpik-添加文件名也有相同的错误可能重复,但我的xml使用的是encoding=“utf-16”,这在编码中不存在这意味着什么?您的
定义
变量是一个常见的.net字符串对象。另外,为brownser返回UTF-16也不是一个好主意。使用UTF-16有什么特别的原因吗?我不知道为什么我的团队使用UTF-16,使用UTF-16有什么问题吗?Web浏览器和许多应用程序在使用UTF-16时表现不太好。另外,以UTF-8的形式为客户端发送文件也是安全的。