【问题标题】:handling special characters in fetch xml处理 fetch xml 中的特殊字符
【发布时间】:2017-08-10 13:35:22
【问题描述】:

我需要管理“AccountText”变量,所以如果我传递一个带有特殊字符的值,例如“MC&CO”,我应该不会收到错误。

var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" + 
                "<entity name='contact'>" + 
                "<attribute name='contactid' />" + 
                "<filter type='and'><condition attribute='parentcustomerid' operator='eq' name='" + AccountText + "' value='" + AccountID + "' /></filter>" +
                "</entity>" + 
                "</fetch>";

【问题讨论】:

    标签: javascript dynamics-crm microsoft-dynamics fetchxml


    【解决方案1】:

    您可以在将AccountText 包含在查询中之前对其进行编码:

    AccountText = HttpUtility.HtmlEncode(AccountText);
    

    【讨论】:

      【解决方案2】:

      据我所知,FetchXML 使用 XML 的原生特殊字符处理,因此您可以将 & 符号转义为 &amp;amp;

      例如:

      <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
          <entity name="account" >
              <attribute name="accountid" />
              <filter type="and" >
                  <condition attribute="name" operator="eq" value="MC&amp;Co" />
              </filter>
          </entity>
      </fetch>
      

      要对所有特殊字符执行此操作,有几种方法可以为 XML 转义字符串。最快的可能是使用System.Web 库中的 System.Security.SecurityElement.Escape() 方法:

      string xml = "<node>it's my \"node\" & i like it<node>";
      string encodedXml = System.Security.SecurityElement.Escape(xml);
      

      输出: &amp;lt;node&amp;gt;it&amp;apos;s my &amp;quot;node&amp;quot; &amp;amp; i like it&amp;lt;node&amp;gt;

      还有更多例子here

      【讨论】:

      • 谢谢你,Aron,但我想要更通用的东西,可以处理所有的 expetions。
      • 我没有使用 .net/c#。
      • 您可能还想标记您正在使用的语言。
      • 啊..对不起。我正在使用JS
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      相关资源
      最近更新 更多