1

过去,我的 FetchXML 以 xml 格式向我提供结果,但由于我更改服务器,此功能string ret = service.Fetch(fetchXml);不再起作用,所以我不得不求助于另一种解决方案,但这给了我更多工作来构建 XML 文件。

获取字符串示例:

 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();

所以我在这里做的是手动构建 XML 字符串,我知道 CRM 可以以 XML 格式提供结果,我有 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)但这比我的代码给了我更多的工作。还是没有其他解决方案?

4

1 回答 1

1

过去,我使用序列化将对象转换为 XML 并再次转换回来。

转换为 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();
            }
        }

将其转换回实体集合(或其他对象)

        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;
            }
        }
于 2013-09-29T22:21:35.480 回答