Question

I am trying to convert SQL server database table into XML file. I followed this solution. I have created this class as shown in solution

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);
        }
    }

Instead of this:

public ActionResult GetStuffAsXml(int id)
{
    var dbStuff = db.GetStuff(id);
    // fetch stuff in database
    return new XmlResult(dbStuff);
}

I have written this(my purpose is to get all products):

public ActionResult Transfer()
        {
            var product = from s in db.Product
                      select s;
            // fetch stuff in database
            return new XmlResult(product);
        }

In debugging process, this error came out:

To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. System.Data.Entity.Infrastructure.DbQuery`1[[Overstock.Models.Product, Overstock, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] does not implement Add(System.Object).

Source error:

Line 42: var xs = new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType(), root);

I assume that error is coming out because I am taking products in wrong way:

var product = from s in db.Product select s;

In what form I should send data to XmlResult class in order to convert SQL Server table to XML file format?

Was it helpful?

Solution

If 'db' is a DbContext in this method

var product = from s in db.Product
                  select s;
// fetch stuff in database
return new XmlResult(product);

Then you are not getting out a DataRow or DataTable, you're getting a collection of strongly typed classes. If you want make xml from them use this code:

public static string SerializeAsXml<T>(T element)
{
    XmlSerializer xmlSerializer = new XmlSerializer(element.);
    StringWriter textWriter = new StringWriter();

    xmlSerializer.Serialize(textWriter, element.GetType());
    return textWriter.ToString();
}

call it

var products = from s in db.Product
                  select s;
// fetch stuff in database
return SerializeAsXml(products);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top