Serialize Entity Framework object with children to XML file

前端 未结 2 810
半阙折子戏
半阙折子戏 2020-12-16 04:55

I\'m querying data with parent/child result sets using Entity Framework and I want to export this data to an XML document.

var agreement = storeops.Agreement         


        
2条回答
  •  心在旅途
    2020-12-16 05:42

    I finally figured out a solution to this but it does require editing the generated classes :(

    Create POCO generated entity classes, set Lazy Loading to true which will get the parent and all children with one select (without having to use Include or Load).

    On the parent class modify the child accessor type from ICollection to FixupCollection.

    public virtual FixupCollection AgreementItemLogs

    Then in the XmlSerializer you have to specify the parent type and child types from the proxy classes.

    var agreement = storeops.Agreements.Include("AgreementItems").SingleOrDefault(a => a.AgreementNumber == AgreementTextBox.Text);                                             
                    var typeList = new List();
    
                    if(agreement.AgreementItems.Count > 0)
                        typeList.Add(agreement.AgreementItems.FirstOrDefault().GetType());
                    if (agreement.AgreementItemLogs.Count > 0)
                        typeList.Add(agreement.AgreementItemLogs.FirstOrDefault().GetType());
                    if (agreement.AgreementPricings.Count > 0)
                        typeList.Add(agreement.AgreementPricings.FirstOrDefault().GetType());
                    if (agreement.AgreementSnapshots.Count > 0)
                        typeList.Add(agreement.AgreementSnapshots.FirstOrDefault().GetType());
                    if (agreement.AgreementTransactions.Count > 0)
                        typeList.Add(agreement.AgreementTransactions.FirstOrDefault().GetType());
                    if (agreement.AgreementTransactionLogs.Count > 0)
                        typeList.Add(agreement.AgreementTransactionLogs.FirstOrDefault().GetType());
    
                    XmlSerializer serializer = new XmlSerializer(agreement.GetType(), typeList.ToArray());
                    XmlWriter writer = XmlWriter.Create("Agreement.xml");
                    serializer.Serialize(writer, agreement);
    

提交回复
热议问题