1
votes

In the past my FetchXML was delivering me the result in xml format, but since I change server this function string ret = service.Fetch(fetchXml); no longer works, so I had to resort with another solution, but this one give me more work to build a XML file.

Fetch String example:

 string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
        <entity name='account'>
        <attribute name='name'/>
        <attribute name='telephone1'/>
        </entity>
        </fetch>";

        EntityCollection ec = organizationProxy.RetrieveMultiple(new FetchExpression(fetchXml));


        XElement rootXml = new XElement("account");
        foreach (Entity account in ec.Entities)
        {
            if (account.Attributes.Contains("name"))
            {
                rootXml.Add(new XElement("name", account.Attributes.Contains("name") ? account["name"] : ""));
                rootXml.Add(new XElement("telephone1", account.Attributes.Contains("telephone1") ? account["telephone1"] : ""));
            }
        }

        res.XmlContent = rootXml.ToString();

So what I'm doing here is build the XML string by hand, and I know that CRM can deliver the result in XML, I have googleit (http://social.msdn.microsoft.com/Forums/en-US/af4f0251-7306-4d76-863d-9508d88c1b68/dynamic-crm-2011-fetchxml-results-into-xmltextreader-to-build-an-xml-output) But this give me more work than my code. Or there is no other solution?

1

1 Answers

1
votes

In the past I have used Serialization to convert objects to XML and back again.

To convert to XML

        public static string SerializeAnObject(object _object)
        {
            System.Xml.XmlDocument doc = new XmlDocument();
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(_object.GetType());
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            try
            {
                serializer.Serialize(stream, _object);
                stream.Position = 0;
                doc.Load(stream);
                return doc.InnerXml;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }

To convert it back into an Entity Collection (or other object)

        public static object DeSerializeAnObject(string xmlOfAnObject, Type _objectType)
        {
            System.IO.StringReader read = new StringReader(xmlOfAnObject);
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(_objectType);
            System.Xml.XmlReader reader = new XmlTextReader(read);
            try
            {
                return (object)serializer.Deserialize(reader);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                read.Close();
                read.Dispose();
                read = null;
            }
        }