[英]Return XML document with REST API
I would like to return a xml document from rest api request: 我想从rest api请求返回一个xml文档:
[HttpPost]
public string getClassXml(HttpRequestMessage req)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
ClassXML classid = new ClassXML();
XmlDocument doc = new XmlDocument();
try
{
var data = req.Content.ReadAsStringAsync().Result;
classid = serializer.Deserialize<ClassXML>(data.ToString().Trim());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
string path = ASDb.ReadValue("SELECT definitionxml FROM alclass WHERE classid='" + classid.classID + "'").ToString();
XmlTextReader reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "Resource\\" + percorso);
reader.Read();
doc.Load(reader);
return doc.innerXml;
}
But in this way I get a string, I would like to have a XmlDocument not a string. 但是通过这种方式,我得到了一个字符串,我想拥有一个XmlDocument而不是一个字符串。 I tried also to return the XmlDocument doc, but it gives me an error: he 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; 我也尝试返回XmlDocument文档,但是它给了我一个错误:“ ObjectContent`1”类型未能序列化内容类型“ application / xml”的响应主体; charset=utf-8'. charset = utf-8'。 Do you have any ideas? 你有什么想法?
As someone wrote here just a few seconds ago (but then deleted his answer) the problem is that XmlDocument is not serializable, if you use XmlElement instead is ok. 正如有人在几秒钟前在这里写的(但随后删除了他的答案)那样,问题是XmlDocument无法序列化,如果您使用XmlElement则可以。 Here is what I did: 这是我所做的:
[HttpPost]
public XmlElement getClassXml(HttpRequestMessage req)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
ClassXML classid = new ClassXML();
XmlDocument doc = new XmlDocument();
try
{
var data = req.Content.ReadAsStringAsync().Result;
classid = serializer.Deserialize<ClassXML>(data.ToString().Trim());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
string path = ASDb.ReadValue("SELECT definitionxml FROM alclass WHERE classid='" + classid.classID + "'").ToString();
XmlTextReader reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "Resource\\" + percorso);
reader.Read();
doc.Load(reader);
XmlElement element = doc.DocumentElement;
return element;
}
Perhaps the issue isn't with your API layer, but when you're trying to use your XmlTextReader? 也许问题不在于您的API层,而是当您尝试使用XmlTextReader时?
XmlTextReader reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "Resource\\\\" + percorso);
What does your XML that you're trying to read in look like? 您尝试读取的XML是什么样的? Have you checked that it's well formed? 您检查过格式是否正确?
In terms of "returning XML document with REST API", I would suggest that you just output the XML document as a string with the appropriate MIME type, doing something like: 关于“使用REST API返回XML文档”,我建议您仅将XML文档输出为具有适当MIME类型的字符串,方法如下:
[HttpPost]
public HttpResponseMessage getClassXml(HttpRequestMessage req)
{
...
XmlTextReader reader = new XmlTextReader(AppDomain.CurrentDomain.BaseDirectory + "Resource\\" + percorso);
reader.Read();
doc.Load(reader);
HttpResponseMessage response = new HttpResponseMessage { Content = new StringContent(doc.innerXml, Encoding.UTF8,"application/xml") };
return response;
}
REST API outputs should map to standard internet mime types (eg JSON data, images, text, etc - not XmlDocument). REST API输出应映射到标准的Internet MIME类型(例如JSON数据,图像,文本等,而不是XmlDocument)。 Whatever is consuming your REST API can just take the text and turn it into a XmlDocument if necessary. 无论使用哪种REST API,只要有必要,就可以获取文本并将其转换为XmlDocument。
As an aside, you don't seem to even use half the code in the example you provided and you can probably clean it up: 顺便说一句,您似乎甚至没有使用所提供示例中的一半代码,并且可以清理它:
string path = ASDb.ReadValue("SELECT definitionxml FROM alclass WHERE classid='" + classid.classID + "'").ToString();
抱歉,但是如果我现在想从HttpPost更改为HttpGet,如何在以下URL中获取参数: http://localhost/arcosat/api/ws/GetClassXml?classid=myclass
我想获取“ myclass”字符串,但是与req.Content.ReadAsStringAsync().Result
不再起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.