简体   繁体   中英

XML Serialization with Navigation Properties

I´ma pretty new MVC developer, and I´m having a little trouble with serializing to XML my classes.

I currently have the following classes:

  public class UserClass
{

    public int UserId{ get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool LogicalDelete { get; set; }

    public virtual ICollection<Phone> Phone{ get; set; }
    [XmlIgnore]
    public virtual ICollection<EventList> Event{ get; set; }
}


public class Phone
{
    public int TelefonosId { get; set; }
    public string Phone{ get; set; }
    public bool Mobile{ get; set; }

    public int UsuarioId { get; set; }
    public virtual UserClass User { get; set; }
}

The serializer method I´m calling from the UserController is the following:

public void ExportToXML()
   {
        var data = mydb.User.ToList();

        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=testXML.xml");
        Response.ContentType = "text/xml";

        var serializer = new System.Xml.Serialization.XmlSerializer(data.GetType());
        serializer.Serialize(Response.OutputStream, data);
   }

And then comes the issue. When I try to serialize, the navigation properties from the User class give me a reflecting type error on the "GetType" call. It works just fine without them (I was able to export the User list correctly, without Phones).

What am I missing? Is there something I could be doing better?

Thanks in advance!

You have to replace the interface ICollection with an implementation of this interface.

For example, replace:

public virtual ICollection<Phone> Phone{ get; set; }

with:

public virtual List<Phone> Phone{ get; set; }

Or you can also implement the IXmlSerializable in the UserClass and describe how to serialize this collection by providing your own serialization logic.

I managed to solve the problem in the following way:

XDocument xmlDocument = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),

                new XComment("Exporting Users to XML"),

                new XElement("Users",

                    from usu in db.Users.ToList()
                    select new XElement("User", new XElement("Email", usu.Email),
                                new XElement("FirstName", usu.FirstName),
                                new XElement("LastName", usu.LastName),
                                new XElement("Deleted", usu.LogicalDelete),
                                  from tel in usu.Phones.ToList()
                                  select new XElement("Phone",
                                new XElement("Phone", tel.Phone),
                                new XElement("Mobile", tel.Mobile)))
                            ));
            xmlDocument.Save("D:\\user.xml");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM