Here is a very good simple way to convert a list of objects to XML.
First, create a class for your list of objects. This class could be declared next to your routine if it is just to capture query results for example.
|
1 2 3 4 5 6 7 |
[Serializable()] public class QR { public string Name { get; set; } public string Address { get; set; } public string Street { get; set; } } |
Here is an example of populating the list from a BC AX query.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
List<QR> QueryResults = new List<QR>(); DynRec = _DynAx.CreateAxaptaRecord("smmBusRelTable"); string fieldName = ("BusRelAccount"); string fieldValue = ("BRC00001"); DynRec.ExecuteStmt(string.Format("select * from %1 where %1.{0} == '{1}'", fieldName, fieldValue)); while (DynRec.Found) { QR newRecord = new QR(); newRecord.Name = (string)DynRec.get_Field("Name"); newRecord.Address = (string)DynRec.get_Field("Address"); newRecord.Street = (string)DynRec.get_Field("Street"); QueryResults.Add(newRecord); DynRec.Next(); } |
And… heres the routine to convert this list to XML.
|
1 2 3 4 5 |
var xs = new XmlSerializer(QueryResults.GetType()); var xml = new StringWriter(); xs.Serialize(xml, QueryResults); xml.ToString(); |
And… finally to Deserialize the XML data;
|
1 2 3 4 5 6 7 |
StringReader rdr = new StringReader(XMLDataString); XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<QR>)); var result = (List<QR>)xmlSerializer.Deserialize(rdr); MessageBox.Show(result[0].Name); |