解决方案 »

  1.   

    调试的时候xResult 里面有值
      

  2.   


        public class Student 
        {
            public int No { get; set; }
            public string Name { get; set; }
        }    public class XmlResult : ActionResult
        {
            public XmlResult(Object data)
            {
                this.Data = data;
            }
            public Object Data
            {
                get;
                set;
            }
            public override void ExecuteResult(ControllerContext context)
            {
                if (Data == null)
                {
                    new EmptyResult().ExecuteResult(context);
                    return;
                }
                context.HttpContext.Response.ContentType = "application/xml";
                using (MemoryStream ms = new MemoryStream())
                {
                    XmlSerializer xs = new XmlSerializer(Data.GetType());
                    xs.Serialize(ms, Data);
                    ms.Position = 0;
                    using (StreamReader sr = new StreamReader(ms))
                    {
                        context.HttpContext.Response.Output.Write(sr.ReadToEnd());
                    }
                }
            }
        }
     public ActionResult ShowXML() 
            {
                List<Student> students = new List<Student>() { new Student() { No = 1, Name = "x" }, new Student() { No = 2, Name = "h" } };            XmlResult xmlResult = new XmlResult(students);
                return xmlResult;
            }成功输出 <?xml version="1.0" ?> 
    - <ArrayOfStudent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    - <Student>
      <No>1</No> 
      <Name>x</Name> 
      </Student>
    - <Student>
      <No>2</No> 
      <Name>h</Name> 
      </Student>
      </ArrayOfStudent>