Prev: encoding
Next: Compiler Error when using VB Namespace
From: AA2e72E on 19 Mar 2010 04:31 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.
From: AA2e72E on 19 Mar 2010 05:08 I have another related question: given myFile, it is possible to query the list of custom values in it e.g. 'name' and 'fs'?
From: Family Tree Mike on 19 Mar 2010 07:19 "AA2e72E" wrote: > 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
From: Alberto Poblacion on 19 Mar 2010 07:21 "AA2e72E" <AA2e72E(a)discussions.microsoft.com> wrote in message news:59B23993-C1C7-4ED6-8EDE-8DD8C92D4B4E(a)microsoft.com... > 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()
From: Alberto Poblacion on 19 Mar 2010 07:23
"AA2e72E" <AA2e72E(a)discussions.microsoft.com> wrote in message news:3BE142D1-C1B6-47C0-B74D-7A69C8A6016C(a)microsoft.com... >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); } |