【问题标题】:How do I access the result of FetchXML as XML rather than entities?如何以 XML 而非实体的形式访问 FetchXML 的结果?
【发布时间】:2013-09-26 16:21:07
【问题描述】:

过去,我的 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) 但这比我的代码给了我更多的工作。还是没有其他解决办法?

【问题讨论】:

    标签: dynamics-crm-2011 fetchxml


    【解决方案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;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多