{"id":533,"date":"2012-09-18T10:49:13","date_gmt":"2012-09-18T10:49:13","guid":{"rendered":"http:\/\/foxware.co.uk\/?p=533"},"modified":"2012-09-18T13:02:50","modified_gmt":"2012-09-18T13:02:50","slug":"serialize-a-list-to-xml","status":"publish","type":"post","link":"http:\/\/foxware.co.uk\/?p=533","title":{"rendered":"Serialize a List to XML"},"content":{"rendered":"<p>Here is a very good simple way to convert a list of objects to XML.<\/p>\n<p>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.<\/p>\n<pre> [Serializable()]\r\n    public class QR\r\n    {\r\n        public string Name { get; set; }\r\n        public string Address { get; set; }\r\n        public string Street { get; set; }\r\n    }<\/pre>\n<p>Here is an example of populating the list from a BC AX query.<\/p>\n<pre>      List&lt;QR&gt; QueryResults = new List&lt;QR&gt;();\r\n\r\n            DynRec = _DynAx.CreateAxaptaRecord(\"smmBusRelTable\");\r\n\r\n            string fieldName = (\"BusRelAccount\");\r\n            string fieldValue = (\"BRC00001\");\r\n\r\n            DynRec.ExecuteStmt(string.Format(\"select * from %1 where %1.{0} == '{1}'\", fieldName, fieldValue));\r\n\r\n            while (DynRec.Found)\r\n            {\r\n                QR newRecord = new QR();\r\n                newRecord.Name      = (string)DynRec.get_Field(\"Name\");\r\n                newRecord.Address   = (string)DynRec.get_Field(\"Address\");\r\n                newRecord.Street    = (string)DynRec.get_Field(\"Street\");\r\n                QueryResults.Add(newRecord);\r\n                DynRec.Next();\r\n            }<\/pre>\n<p>And&#8230; heres the routine to convert this list to XML.<\/p>\n<pre>var xs = new XmlSerializer(QueryResults.GetType());\r\nvar xml = new StringWriter();\r\nxs.Serialize(xml, QueryResults);\r\n\r\nxml.ToString();<\/pre>\n<p>And&#8230; finally to Deserialize the XML data;<\/p>\n<pre>StringReader rdr = new StringReader(XMLDataString);\r\n\r\nXmlSerializer xmlSerializer = new XmlSerializer(typeof(List&lt;QR&gt;));\r\n\r\nvar result = (List&lt;QR&gt;)xmlSerializer.Deserialize(rdr);\r\n\r\nMessageBox.Show(result[0].Name);<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"http:\/\/foxware.co.uk\/?p=533\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/533"}],"collection":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=533"}],"version-history":[{"count":3,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions"}],"predecessor-version":[{"id":535,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions\/535"}],"wp:attachment":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}