关于dynamics crm:无效的XML。

Invalid XML. ---&gt On A Plugin Containing FetchXML

当合同名称中存在&;或其他无效字符时,会发生这种情况。我的问题是,如何处理这些无效字符??!!

Microsoft.Crm.CrmException: Invalid XML. --->
System.Xml.XmlException: An error occurred while parsing EntityName.
Line 13, position 117.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32&
charRefEndPos)
at System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr)
at System.Xml.XmlTextReaderImpl.ParseAttributes()
at System.Xml.XmlTextReaderImpl.ParseElement()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at Microsoft.Crm.Platform.Server.Utility.XmlHelper.LoadXmlInfo(String
xmlInfo)
at Microsoft.Crm.Query.EntityExpression.ExtractPlatformName(String fetchXml, XElement element).

以下是问题代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//run a fetchXml query to get how many contract lines have these values

string fetchLines = @"
  <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    <entity name='new_yummycontractline'>
     
      <filter type='and'>
        <condition attribute='new_servingtime' operator='eq' value='" + servingTime.ToString() + @"' />
        <condition attribute='new_servinggroup' operator='eq' value='" + servingGroup.ToString() + @"' />
        <condition attribute='new_destination' operator='eq' value='" + location.ToString() + @"' />
        <condition attribute='statecode' operator='eq' value='0' />
      </filter>
      <link-entity name='new_yummycontract' from='new_yummycontractid' to='new_contractid' link-type='inner' alias='ad'>
        <filter type='and'>
          <condition attribute='new_yummycontractid' operator='eq' uiname='" + uiname + @"' uitype='new_yummycontract' value='" + contractId.ToString() + @"' />
        </filter>
      </link-entity>
    </entity>
  </fetch>";

EntityCollection results = service.RetrieveMultiple(new FetchExpression(fetchLines));


fetchxml工作不需要uinameuitype。它们用于表示,而不是用于查询。我认为这些是由高级查找编辑器添加的;因此"ui"

您可以这样重写XML的那个部分

1
2
3
4
5
<link-entity name='new_yummycontract' from='new_yummycontractid' to='new_contractid' link-type='inner' alias='ad'>
  <filter type='and'>
    <condition attribute='new_yummycontractid' operator='eq' value='" + contractId.ToString() + @"' />
  </filter>
</link-entity>

不确定您使用的是.NET的哪个版本,但是您也可以使用$特殊字符来表示插入字符串,我认为这可以提高可读性并减少字符串连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string fetchLines = @$"
  <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    <entity name='new_yummycontractline'>
     
      <filter type='and'>
        <condition attribute='new_servingtime' operator='eq' value='{servingTime}' />
        <condition attribute='new_servinggroup' operator='eq' value='{servingGroup}' />
        <condition attribute='new_destination' operator='eq' value='{location}' />
        <condition attribute='statecode' operator='eq' value='0' />
      </filter>
      <link-entity name='new_yummycontract' from='new_yummycontractid' to='new_contractid' link-type='inner' alias='ad'>
        <filter type='and'>
          <condition attribute='new_yummycontractid' operator='eq' value='{contractId}' />
        </filter>
      </link-entity>
    </entity>
  </fetch>";

我用了webutility.htmlencode(uiname),问题似乎已经消失了。我猜uiname有一些无效的xml字符,这个方法可以解决这个问题!