View Code
View Code
/* *根据属性名称读取属性值 */ function GetAttributeValue(entityNode, attrName) { var attributes = entityNode.selectNodes('//a:KeyValuePairOfstringanyType') for (var i = 0; i < attributes.length; i++) { var key = attributes[i].selectSingleNode('b:key').text; if (key == attrName) { var attribute = attributes[i]; var itype = attribute.selectSingleNode('b:value').getAttribute('i:type') switch (itype) { case 'a:EntityReference': return { Id: attribute.selectSingleNode('b:value/a:Id').text, LogicalName: attribute.selectSingleNode('b:value/a:LogicalName').text, Name: attribute.selectSingleNode('b:value/a:Name').text }; break; case 'a:OptionSetValue': return { Value: attribute.selectSingleNode('//a:Value').text }; break; default: } } } }
昨天测试完后对代码做了一些调整,补充一些default
/* *根据属性名称读取属性值 */ function GetAttributeValue(entityNode, attrName) { var attributes = entityNode.selectNodes('//a:KeyValuePairOfstringanyType') for (var i = 0; i < attributes.length; i++) { var key = attributes[i].selectSingleNode('b:key').text; if (key == attrName) { var attribute = attributes[i]; var itype = attribute.selectSingleNode('b:value').getAttribute('i:type') switch (itype) { case 'a:EntityReference': return { Id: attribute.selectSingleNode('b:value/a:Id').text, LogicalName: attribute.selectSingleNode('b:value/a:LogicalName').text, Name: attribute.selectSingleNode('b:value/a:Name').text }; break; case 'a:OptionSetValue': return { Value: attribute.selectSingleNode('//a:Value').text }; break; default: return attribute.selectSingleNode('b:value').text; break; } } } }
<a:Entity xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts"> <a:Attributes xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"> <a:KeyValuePairOfstringanyType> <b:key>new_backupid</b:key> <b:value xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="c:guid" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/">e27f1c40-7ffc-e111-90d0-00155d77394d</b:value> </a:KeyValuePairOfstringanyType><a:KeyValuePairOfstringanyType><b:key>new_account_new_backup_id</b:key><b:value xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="a:EntityReference"><a:Id>091d12c9-cec4-e111-ab5c-00155d77394d</a:Id><a:LogicalName>account</a:LogicalName><a:Name>ACE Life Insurance Company Limited</a:Name></b:value></a:KeyValuePairOfstringanyType><a:KeyValuePairOfstringanyType><b:key>new_type</b:key><b:value xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="a:OptionSetValue"><a:Value>100000000</a:Value></b:value></a:KeyValuePairOfstringanyType> <a:KeyValuePairOfstringanyType><b:key>new_systemuser_new_backup_id</b:key><b:value xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:type="a:EntityReference"><a:Id>a0534b18-b5c4-e111-ab5c-00155d77394d</a:Id><a:LogicalName>systemuser</a:LogicalName><a:Name>方力</a:Name></b:value></a:KeyValuePairOfstringanyType></a:Attributes><a:EntityState xmlns:i="http://www.w3.org/2001/XMLSchema-instance" i:nil="true"/><a:FormattedValues xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"><a:KeyValuePairOfstringstring> <b:key>new_type</b:key><b:value>临分个险核保A</b:value></a:KeyValuePairOfstringstring></a:FormattedValues><a:Id>e27f1c40-7ffc-e111-90d0-00155d77394d</a:Id> <a:LogicalName>new_backup</a:LogicalName> <a:RelatedEntities xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"/> </a:Entity>