Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Invalid XML exception

(0) ShareShare
ReportReport
Posted on by 15

I am getting invalid XML error can anyone help me

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Query;
using System.Web.UI.WebControls;
using Microsoft.Xrm.Sdk.WebServiceClient;


namespace postoperationonaccount
{
public class postoperation : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);

try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];

if (entity.LogicalName == "account")
{
var leadoriginid = entity.GetAttributeValue<EntityReference>("originatingleadid").Id;
Console.WriteLine(leadoriginid);

if (leadoriginid != null)
{
StringBuilder fetch = new StringBuilder();
fetch.Append("<fetch version='1.0' output-format='xml - platform' mapping='logical' distinct='true'>");
fetch.Append(" <entity name='competitor'>");
fetch.Append("<attribute name='name' />");
fetch.Append("<attribute name='websiteurl' />");
fetch.Append("<attribute name='competitorid' />");
fetch.Append("<order attribute='name' descending='false' />");
fetch.Append("<link-entity name='leadcompetitors' from='competitorid' to='competitorid' visible='false' intersect='true'>");
fetch.Append("<link-entity name='lead' from='leadid' to='leadid' alias='ag'>");
fetch.Append("<filter type='and'>");
fetch.Append("<condition attribute='leadid' operator='eq' value='{"+leadoriginid.ToString()+"}' />");
fetch.Append("</ filter>");
fetch.Append("</link-entity>");
fetch.Append("</link-entity>");
fetch.Append("</entity>");
fetch.Append("</fetch>");

// var query=string.Format(fetchxml, leadoriginid.ToString());
EntityCollection competitorresult = service.RetrieveMultiple(new FetchExpression(fetch.ToString()));
Console.WriteLine(competitorresult);
var relatedentities = new EntityReferenceCollection();
foreach (var e in competitorresult.Entities)
{
relatedentities.Add(new EntityReference("competitor", e.Id));

}
var relationship = new Relationship("sb_competitor_account");
Guid entityid = entity.Id;
service.Associate("account", entityid, relationship, relatedentities);
}
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}


  • oliver.rodrigues Profile Picture
    4,052 on at
    RE: Invalid XML exception

    please review the tags used in the thread, tagging all of options won't get your answer quicker

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,393 User Group Leader on at
    RE: Invalid XML exception

    Hi Preetham!

    @Andrey is right. You don't have to pass the dynamic values with curly braces. Also, you have extra space as @Andrey shown in the screenshot.

  • Suggested answer
    Kipetcoff Profile Picture
    1,037 on at
    RE: Invalid XML exception

    Here is another problem place

    2019_2D00_10_2D00_16_5F00_20_2D00_28_2D00_38.png

  • Suggested answer
    Kipetcoff Profile Picture
    1,037 on at
    RE: Invalid XML exception

    Hi,

    Try to write condition without {}

  • Preetham Holenarasipuradeveraju Profile Picture
    15 on at
    RE: Invalid XML exception

    I am passing a value dynamically leadoriginid my value

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106 Most Valuable Professional

#3
Eugen Podkorytov Profile Picture

Eugen Podkorytov 95

Overall leaderboard

Product updates

Dynamics 365 release plans