无效的XML。 --->在包含FetchXML的插件上

时间:2018-11-11 20:45:27

标签: dynamics-crm dynamics-365 fetchxml

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

  

Microsoft.Crm.CrmException:无效的XML。 ->   System.Xml.XmlException:解析EntityName时发生错误。   第13行,位置117 。       在System.Xml.XmlTextReaderImpl.Throw(Exception e)       在System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue,EntityExpandType expandType,Int32&   charRefEndPos)       在System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos,Char quoteChar,NodeData attr)       在System.Xml.XmlTextReaderImpl.ParseAttributes()       在System.Xml.XmlTextReaderImpl.ParseElement()       在System.Xml.XmlTextReaderImpl.ParseElementContent()       在System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)       在System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r,LoadOptions o)       在System.Xml.Linq.XDocument.Load处(XmlReader阅读器,LoadOptions选项)       在Microsoft.Crm.Platform.Server.Utility.XmlHelper.LoadXmlInfo(String   xmlInfo)       在Microsoft.Crm.Query.EntityExpression.ExtractPlatformName(字符串fetchXml,XElement元素)处。

这是有疑问的代码:

//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'>
      <attribute name='new_yummycontractlineid' />
      <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));

2 个答案:

答案 0 :(得分:1)

uinameuitype不需要fetchXml起作用。这些用于演示而不用于查询。我认为这些是由“高级查找”编辑器添加的;因此是“ UI”

您可以像这样重写XML的这一部分

<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版本,但是您也可以使用$特殊字符来表示一个interpolated string,我认为这可以提高可读性并减少字符串连接

string fetchLines = @$"
  <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    <entity name='new_yummycontractline'>
      <attribute name='new_yummycontractlineid' />
      <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>";

答案 1 :(得分:0)

我使用了webutility.htmlencode(uiname),问题似乎消失了。我猜uiname包含一些无效的XML字符,并且该方法解决了这个问题!