代码之家  ›  专栏  ›  技术社区  ›  so cal cheesehead

无法创建XElement值包含“:”的XML

  •  0
  • so cal cheesehead  · 技术社区  · 3 年前

    我有以下代码:

        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();
        }
    

    这将生成所需的XML,但是我遇到了以下错误:

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

    由于的价值 runInfo.FieldValue 其中包含 : 例如,该值可能看起来像:

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

    到目前为止,我看到的所有解决方案或类似问题都围绕着生成正确的元素名称,我的问题是围绕着值。例如,如果我删除 : 来自 runInfo。字段值 变量,则生成预期的XML。

    有什么想法吗?我尝试过对字符串进行URL编码,但这只会导致类似的错误,只是它抱怨 % 价值观

    0 回复  |  直到 3 年前