我正在尝试模拟我无法访问的服务器进行测试。服务器返回我正在测试的客户端读取并加载到 XmlDocument 类中的 XML 文件的内容。 看起来应该很容易读取 xml 文件并返回其内容,但我似乎无法弄清楚。如果我将文件加载到 XmlDocument 并返回 InnerXML,则控制器会将结果包装在根中,而客户端上的 XmlDocument 会阻止它。
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><?xml version="1.0" encoding="utf-8"?><credential xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><user>User</user><password>Password</password><cookie>Cookie Text</cookie><site xmlns="vs">VS Site</site><funcList>func1, func2, func3</funcList></credential></string>
如果我尝试返回 XmlDocument,控制器会抛出异常:“System.Xml.XmlDocument”是无效的集合类型,因为它没有参数类型为“System.Object”的有效 Add 方法。
using Newtonsoft.Json;
using POSComWebTest.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Http;
using System.Xml;
using System.Xml.Serialization;
namespace WshhWebApi.Controllers
{
[RoutePrefix("SiteController")]
public class AuthorizeController : ApiController
{
[Route("cgi-bin/CGILink")]
[HttpGet]
public IHttpActionResult CGILink(string cmd, string user, string passwd)
{
try
{
string file = Path.Combine(HttpContext.Current.Server.MapPath("~"), "App_Data", "credential.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(file);
HttpContext.Current.Response.ContentType = "application/xml";
//return Content(System.Net.HttpStatusCode.OK, xmlDoc.InnerText); // garbage
return Content(System.Net.HttpStatusCode.OK, xmlDoc.InnerXml); // <string root> client can't read as xml
//return Content(System.Net.HttpStatusCode.OK, xmlDoc); // 'System.Xml.XmlDocument' is an invalid collection type since it does not have a valid Add method with parameter of type 'System.Object'.
}
catch (Exception ex)
{
return BadRequest($"CGILink Failed: {ex.Message}");
}
}
}
}
看起来这应该很容易,我希望我没有用谷歌搜索错误的问题。谢谢!
哦,这是我的 XML 文件 credential.xml:
<?xml version="1.0" encoding="utf-8"?>
<credential xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<user>User</user>
<password>Password</password>
<cookie>Cookie Text</cookie>
<site xmlns="vs">VS Site</site>
<funcList>func1, func2, func3</funcList>
</credential>
首先感谢大家的回答!我想我明白了。如果我只是将文件下载为“application/octet-stream”,客户端就可以将其转换为 XmlDocument。我真的不认为这会起作用,但我在这上面花了太多时间并且越来越绝望。以下是我的解决方案:
[Route("cgi-bin/CGILink")]
[HttpGet]
public IHttpActionResult CGILink(string cmd, string user, string passwd)
{
try
{
string file = Path.Combine(HttpContext.Current.Server.MapPath("~"), "App_Data", "credential.xml");
using (FileStream fileStream = File.OpenRead(file))
{
MemoryStream memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(memStream.GetBuffer())
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var response = ResponseMessage(result);
return response;
}
}
catch (Exception ex)
{
return BadRequest($"CGILink Failed: {ex.Message}");
}
}
再次感谢所有回复的人。