Serialization/LINQ

  • Thread starter Thread starter AA2e72E
  • Start date Start date
A

AA2e72E

Given

var myFile = (from file in fileList // Most recent file
orderby file.CreationTime descending
select new { file.FullName, fs = file.Length }).First();

- What is the easiest way to serialize myFile? (need the XML to show the
name/value pair for each value)
- What is the easiest way to SOAP serialize myFile? (need the XML to show
the name/value pair for each value as well as the type i.e string for
FullName & int for fs)

I was hoping for the var myFile object to expose a Serialize method but I
can't find one.

Thanks for your help.
 
I have another related question:

given myFile, it is possible to query the list of custom values in it e.g.
'name' and 'fs'?
 
AA2e72E said:
Given

var myFile = (from file in fileList // Most recent file
orderby file.CreationTime descending
select new { file.FullName, fs = file.Length }).First();

- What is the easiest way to serialize myFile? (need the XML to show the
name/value pair for each value)
- What is the easiest way to SOAP serialize myFile? (need the XML to show
the name/value pair for each value as well as the type i.e string for
FullName & int for fs)

I was hoping for the var myFile object to expose a Serialize method but I
can't find one.

Thanks for your help.

I don't have access to VS 2008 or higher where I am at the moment, but
wouldn't the following work?

XmlSerializer ser = new XmlSerializer(myFile.GetType());
ser.Serialize(SomeStream, myFile);

Mike
 
AA2e72E said:
var myFile = (from file in fileList // Most recent file
orderby file.CreationTime descending
select new { file.FullName, fs =
file.Length }).First();

- What is the easiest way to serialize myFile? (need the XML to show the
name/value pair for each value)

The easiest way is probably the XmlSerializer:

XmlSerializer s = new XmlSerializer(typeof(myFile));
TextWriter writer = new StreamWriter(filename);
s.Serialize(writer, myFile);
writer.Close();
- What is the easiest way to SOAP serialize myFile?

You can use runtime serialization by means of the SoapFormatter:

using System.Runtime.Serialization.Formatters.Soap; //Requires adding
Reference
....
SoapFormatter f = new SoapFormatter();
Stream s = new FileStream(...):
f.Serialize(s, myFile)
s.Close()
 
AA2e72E said:
I have another related question:

given myFile, it is possible to query the list of custom values in it e.g.
'name' and 'fs'?

Yes, by means of Reflection:

using System.Reflection;
....
Type t = typeof(myFile);
PropertyInfo[] props = t.GetProperties();
foreach (PropertyInfo pi in props)
{
Console.WriteLine(pi.Name);
}
 
I had already tried your suggestion ........

XmlSerializer ser = new XmlSerializer(myFile.GetType());
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, myFile);

........... but without success; I encounter

<>f__AnonymousType0`2[System.String,System.Int32] cannot be serialized
because it does not have a parameterless constructor.
 
AA2e72E said:
I had already tried your suggestion ........

XmlSerializer ser = new XmlSerializer(myFile.GetType());
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, myFile);

.......... but without success; I encounter

<>f__AnonymousType0`2[System.String,System.Int32] cannot be serialized
because it does not have a parameterless constructor.

Ah, yes... That does make sense. I think reflection as Alberto replied
earlier would work.

Mike
 
AA2e72E said:
I had already tried your suggestion ........

XmlSerializer ser = new XmlSerializer(myFile.GetType());
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, myFile);

.......... but without success; I encounter

<>f__AnonymousType0`2[System.String,System.Int32] cannot be serialized
because it does not have a parameterless constructor.


You would need to make a new object call it MyObject and serialize that.

public class MyObject
{
public string FullName {get; set;}
public int FileLength {get; set;}
}


var myobject = (from file in fileList // Most recent file
orderby file.CreationTime descending
select new MyObject{ FullName = file.FullName,
FileLength = file.Length}).First();

You should be able to serialize MyObject.
 
Back
Top