2017-08-10 108 views
1

我需要管理'AccountText'變量,所以如果我傳遞一個特殊字符的值例如「MC & CO」,我不應該得到一個錯誤。處理提取xml中的特殊字符

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>"; 

回答

1

據我所知FetchXML使用XML的本地特殊字符處理,所以你能逃脫符號爲&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.Security.SecurityElement.Escape()方法在System.Web庫:

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

,其輸出: &lt;node&gt;it&apos;s my &quot;node&quot; &amp; i like it&lt;node&gt;

有更多的例子here

+0

謝謝Ar上,但我想要更通用的東西,以處理所有的考驗。 – user2173466

+0

我沒有使用.net/c#。 – user2173466

+1

您可能還想標記您正在使用的語言。 – Aron

3

你可以編碼AccountText在將其包括在查詢中之前:

AccountText = HttpUtility.HtmlEncode(AccountText);