У меня есть модель, которую я хотел бы сериализовать в XML с определенными свойствами.
Модель:
public class MyClassModel
{
public int Id { get; set; }
public DateTime updated { get; set; }
}
Код в действии контроллера:
IList<MyClassModel> objects = getStuff();
return new XmlResult(jaPropEstates); //Asp.net mvc class that is inherited from ActionResult
Класс 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();
var xs = new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType());
context.HttpContext.Response.ContentType = "text/xml";
xs.Serialize(context.HttpContext.Response.Output, this.objectToSerialize);
}
}
}
Вывод:
<ArrayOfMyClassModel>
<MyClassModel>
<Id>0</Id>
<updated>0001-01-01T00:00:00</updated>
</MyClassModel>
<MyClassModel>
<Id>2</Id>
<updated>0001-01-01T00:00:00</updated>
</MyClassModel>
Я хочу, чтобы это было так:
<?xml version="1.0" encoding="utf-8" ?> <!-- I want this -->
<listings xmlns="listings-schema"> <!-- I want ArrayOfMyClassModel to be renamed to this -->
<property> <!-- I want MyClassModel to be renamed to property -->
<Id>2</Id>
<updated>0001-01-01T00:00:00</updated>
</property>
</listings>
Примечаниеразница как прокомментировано.Как я могу дать своим элементам нестандартные имена?