& Symbol causing error in XML Code

旧巷老猫 提交于 2019-12-07 21:15:40

问题


I have the following XML code that is filtering my Lookup Field in my Crm Dynamics form. The filter is used following the data that is entered into the Account Field. However, the account field can contain the & symbol, and when it does, an error occurs stating that the XML is not well formed.

Does anyone have any solutions to the problem?

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

回答1:


Some characters have special meaning in XML and ampersand (&) is one of them. Consequently, these characters should be substituted (ie use string replacement) with their respective entity references. Per the XML specification, there are 5 predefined entities in XML:

&lt;    <   less than
&gt;    >   greater than
&amp;   &   ampersand 
&apos;  '   apostrophe
&quot;  "   quotation mark

Alternatively, you can place "text" strings that might contain special characters within a CDATA section so XML parsers will not attempt to parse them. Example:

<SomeElement><![CDATA[This & will be ignored]]></SomeElement>


来源:https://stackoverflow.com/questions/26471640/symbol-causing-error-in-xml-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!