& 导致 XML 代码错误的符号

Sjh*_*son 3 xml filter dynamics-crm-2013

我有以下 XML 代码在我的 Crm Dynamics 表单中过滤我的查找字段。过滤器在输入到帐户字段的数据之后使用。但是,帐户字段可以包含&符号,当它包含时,会发生错误,指出 XML 格式不正确。

有没有人有任何解决问题的方法?

function accountcontact()
{
    Xrm.Page.getControl("new_contactlookup").addPreSearch(function () { addcontactlookup(); });

    function addcontactlookup()
    {
        var accountID = Xrm.Page.getAttribute("new_companylookup");
        var AccountIDObj= accountID.getValue();

        if (AccountIDObj != null)
        {
            var fetchFilter1 = "<filter type='and'><condition attribute='parentcustomerid' uitype='" + AccountIDObj[0].entityType + "' operator='eq' value='" + AccountIDObj[0].id + "' uiname='" + AccountIDObj[0].name + "' /></filter>";
            Xrm.Page.getControl("new_contactlookup").addCustomFilter(fetchFilter1);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Hai*_*ian 5

某些字符在 XML 中具有特殊含义,与号 (&) 就是其中之一。因此,这些字符应该用它们各自的实体引用替换(即使用字符串替换)。根据 XML 规范,XML 中有 5 个预定义实体

&lt;    <   less than
&gt;    >   greater than
&amp;   &   ampersand 
&apos;  '   apostrophe
&quot;  "   quotation mark
Run Code Online (Sandbox Code Playgroud)

或者,您可以在 CDATA 部分中放置可能包含特殊字符的“文本”字符串,以便 XML 解析器不会尝试解析它们。例子:

<SomeElement><![CDATA[This & will be ignored]]></SomeElement>
Run Code Online (Sandbox Code Playgroud)