Xml- 시리얼 라이저 만 있으면됩니다. 내가 발견하고 내 XML-응답을 약간 쥐게 한이 XmlResult-클래스를 사용하고 필요 :
public class XmlResult : ActionResult
{
private object objectToSerialize;
/// <summary>
/// Initializes a new instance of the <see cref="XmlResult"/> class.
/// </summary>
/// <param name="objectToSerialize">The object to serialize to XML.</param>
public XmlResult(object objectToSerialize)
{
this.objectToSerialize = objectToSerialize;
}
/// <summary>
/// Gets the object to be serialized to XML.
/// </summary>
public object ObjectToSerialize
{
get { return this.objectToSerialize; }
}
/// <summary>
/// Serialises the object that was passed into the constructor to XML and writes the corresponding XML to the result stream.
/// </summary>
/// <param name="context">The controller context for the current request.</param>
public override void ExecuteResult(ControllerContext context)
{
if (this.objectToSerialize != null)
{
context.HttpContext.Response.Clear();
XmlRootAttribute root = new XmlRootAttribute("response");
var xs = new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType(), root);
context.HttpContext.Response.ContentType = "text/xml";
xs.Serialize(context.HttpContext.Response.Output, this.objectToSerialize);
}
}
를 그리고, 당신이 당신의 작업에서 XML을 반환 할 때마다 간단하게 수행 할 수 있습니다
public ActionResult GetStuffAsXml(int id)
{
var dbStuff = db.GetStuff(id);
// fetch stuff in database
return new XmlResult(dbStuff);
}
희망이 도움이됩니다.
도움이된다. 고마워! 직렬화 된 XML을 객체로 역순으로 변환하는 것은 어떻습니까? – Evan
Xmlserializer의 Deserialize 메서드를 확인하십시오. XML이 개체와 일치하는 한 꽤 간단합니다 ... –