How to map XML document to entity framework object?

.net c# entity-framework entity-framework-6 xml-serialization

Accepted Answer

Every word is spoken in:

Cannot serialize member Proj.Accounting.Entity.DocumentStatus.Documents of type
System.Collections.Generic.ICollection1[[Proj.Accounting.Entity.Document, Proj.Accounting.Entity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface

Interfaces cannot be serialized. Change all of yourICollection attributes of a typeHashSet so that these things can be serialized.

1
9/7/2015 10:11:59 AM

Popular Answer

Serializable

The issue, in my opinion, is that your class Document cannot be serialized. All of an object's fields need to be Serializable in order to serialize it. If you don't want the Documents member to be serialized, mark it with [System.Xml.Serialization.XmlIgnoreAttribute].

Use the methods Serialize and Deserialize to serialize and deserialize objects respectively. You don't require anything more.

Additional details: https://msdn.microsoft.com/es-es/library/system.xml.serialization.xmlserializer(v=vs.110).aspx


Tips

Additionally, switch to using StringBuilder for concatenating strings. Considering that this is test code, keep in mind that you can use the += operator on strings to produce Clean Code.

Examples:

string xml = "";
xml = xml + "<?xml version=\"1.0\" encoding=\"UTF - 8\"?>";
xml = xml + "<root>";

with the += operator (Same result but cleaner)

string xml = "";
xml += "<?xml version=\"1.0\" encoding=\"UTF - 8\"?>";
xml += "<root>";

StringBuilder is utilized (Best and fastest way)

StringBuilder xml = new StringBuilder();
xml.Append("<?xml version=\"1.0\" encoding=\"UTF - 8\"?>");
xml.Append("<root>");


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow