0

I am using XMLSerialization to pass data from a client based Win7 application to our server (Server 2008 R2) via a Pipe communication process. I have been brought into this project to finish the previous developer's efforts who abandoned it ... therefore I am presently trying to fix the C# code in VS2010 that doesn't work.

The problem is that I cannot obtain full serialization of the resulting output from the following method which is in "public partial class Test". We have a table defined in the MS Compact Server database (named "Test") which matches each item below and has a FK to the "Channel" table, based on matching TestID's in both tables. EntityCollection is based on its relationship to the "Channel" table:

public Test(Guid TestID, String TestName, String TestRemarks, String ScriptPath,
            EntityCollection<Channel> TChannels)
{
    ID = TestID;
    Name = TestName;
    Remarks = TestRemarks;
    Path = ScriptPath;
    Channels = TChannels;
}

Here is a sample of how it is used in the execution of the method:

Test T = new Test(NewID, "Test1", "Test_120912-1729",
            "C:\\Temp\\TestScript_16.txt", TChannels);

Here is the result of the serialization process:

<DataCore xsi:type="Test">
  <ID>bc6a8ef7-c31f-404d-8108-86219d45be63</ID>
  <Name>Test1</Name>
  <Remarks>Test_120912-1729</Remarks>
  <Path>C:\Temp\TestScript_16.txt</Path>
</DataCore>

The first four parameters serialize fine, but the last one (the EntityCollection) is failing to do so. However, if I try to serialize "TChannels" by itself (outside the "Test" function) the serialization of each Test Channel works perfectly. I don't fully understand the limitations/requirements for utilizing XMLserialization to solve this problem. Why am I unable to serialize the EntityCollection< > from within the function?

Thanks for your assistance!

4
  • This may be related to XmlSerializer not loading the child entities - they are lazy-loaded by default. What is the status of Channels.IsLoaded? Have you tried Channels.Load() before attempting your serialization? Commented Sep 13, 2012 at 21:20
  • Channels.IsLoaded is "false". Based on your suggestion,w When I try to insert a line prior to Serialization, "Channels.Load();" , the VS compiler states "'Channels' does not contain a definition for 'Load'". This should be a clue to me, however, I don't know what I should do to handle the 'Load()' definition. Thanks for your help to this point! Commented Sep 13, 2012 at 22:13
  • You may have to use LoadProperty instead if your classes aren't EF: msdn.microsoft.com/en-us/library/dd395523 and msdn.microsoft.com/en-us/library/bb896272%28v=vs.100%29. Sorry about not spotting that last time. Another possibility is to replace the plain XmlSerializer with a DataContractSerializer, although I don't think that removes the need to load child relations: stackoverflow.com/questions/6234290/…. Sorry I can't be more specific. Commented Sep 13, 2012 at 23:02
  • Thanks, Dave. I will look into your references and explore my options. Should I change "LazyLoadingEnabled" to "false" in the Model.edmx ( ' <EntityContainer Name="IP_DBEntities" annotation:LazyLoadingEnabled="true"> ' )?? Commented Sep 13, 2012 at 23:16

1 Answer 1

0

I finally found a solution to the above problem. While the many suggestions were appreciated, none of them provided a solution to my inability to XmlSerialize a child EntityCollection. I dug deeper and looked into the ADO.NET Framework and found within the auto generated code in my database Model.Designer.cs file, an [XmlIgnoreAttribute()] near the beginning of Navigation Properties in my Test (EdmEntityTypeAttribute).

I simply removed the [XmlIgnoreAttribute()] line completely and now all the child objects of "Channels" from the EntityCollection<> are Serialized properly. I hope this can help others who are also unable to Serialize child objects.

Thanks @Dave R.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.