0

I have the following code:

    private string GetXmlBody()
    {
        XNamespace ns = "http://schemas.xmlsoap.org/soap/envelope/";
        XNamespace xsdNs = "http://www.w3.org/2001/XMLSchema";
        XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";
        XNamespace outNs = "http://soap.sforce.com/2005/09/outbound";
        XNamespace sfNs = "urn:sobject.enterprise.soap.sforce.com";

        XDocument requestXml = new XDocument(
            new XElement(ns + "Envelope", new XAttribute(XNamespace.Xmlns + "soapenv", ns), new XAttribute(XNamespace.Xmlns + "xsd", xsdNs), new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
                new XElement(ns + "Body",
                    new XElement(outNs + "notifications", new XAttribute("xmlns", outNs),
                        new XElement(outNs + "OrganizationId", runInfo.OrgId),
                        new XElement(outNs + "SessionId", runInfo.SessionId),
                        new XElement(outNs + "EnterpriseUrl", runInfo.Location),
                        new XElement(outNs + "Notification",
                            new XElement(outNs + "Id", "04l0H000014TY73QAG"),
                            new XElement(outNs + "sObject", new XAttribute(XNamespace.Xmlns + "sf", sfNs), new XAttribute(xsiNs + "type", "sf:" + runInfo.RecordType),
                                new XElement(sfNs + "Id", runInfo.RecordId),
                                new XElement(sfNs + runInfo.Field, runInfo.FieldValue)
                            )
                        )
                    )
                )
            )
        );

        return requestXml.ToString();
    }

Which will generate XML needed however I'm running into the following error:

System.Xml.XmlException : The ':' character, hexadecimal value 0x3A, cannot be included in a name.

due to the value of runInfo.FieldValue which contains :. For Example the value may look like:

Opportunity:006i000000sidsh;Account:;userId:a016S00000sjsiq;sandbox:true

So far all the solutions or similar problems that I've seen revolve around producing the correct element name, my problem is around the value. If for instance I remove the : from the runInfo.FieldValue variable then the expected XML is produced.

Any thoughts on how to get around this? I've tried URL encoding the string but that just leads to a similar error except it complains about % values.

so cal cheesehead
  • 2,515
  • 4
  • 29
  • 50
  • Have you tried XMLConvert.EncodeName? https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlconvert.encodename?view=net-5.0 The values need to be escaped when they have characters that are used for XML. – Mr. Cooper Oct 14 '21 at 21:40
  • @Mr.Cooper Yes but I get the same error, that method will convert `;` but not `:` – so cal cheesehead Oct 14 '21 at 21:47
  • So I end up with things like `Opportunity:006i000000sidsh_x003B_Account:_x003B_userId:a016S00000sjsiq_x003B_sandbox:true` – so cal cheesehead Oct 14 '21 at 21:53
  • Use [`XmlConvert.EncodeLocalName(String)`](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlconvert.encodelocalname?view=net-5.0): *This method is similar to the EncodeName method except that it encodes the colon character, which guarantees that the name can be used as the local name part of a namespace qualified name.* – dbc Oct 15 '21 at 00:26
  • See e.g. [How to change the XElement name in C#](https://stackoverflow.com/a/39015465/3744182) and [C# How to update xml file using XDocument class when xml file generated by dataset WriteXml() function](https://stackoverflow.com/q/68313407/3744182). In fact your question may be a duplicate of the former. – dbc Oct 15 '21 at 00:29
  • 1
    Is the given long string really supposed to ba the element *name* (as you code suggests) or rather the element *value* (as your title suggests)? – Klaus Gütter Oct 15 '21 at 04:14
  • The error implies that a *name* contains a colon, not the value - i.e. it's likely within `runInfo.Field` not `runInfo.FieldValue`. There is no restriction on a value containing a colon, and any character requiring escaping would be handled automatically. – Charles Mager Oct 15 '21 at 15:26
  • To highlight, [here's a fiddle](https://dotnetfiddle.net/Py5XJg) of your code where I've hard coded the value in place. There's no issue. It'd be helpful if you could provide a [mcve] the demonstrates your issue. – Charles Mager Oct 15 '21 at 15:46

0 Answers0