c# - 无法创建 XElement 值包含“:”的 XML
问题描述
我有以下代码:
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.FieldValue
变量中删除 ,则生成预期的 XML。
关于如何解决这个问题的任何想法?我已经尝试过对字符串进行 URL 编码,但这只会导致类似的错误,只是它抱怨%
值。
解决方案
推荐阅读
- php - laravel 5.5 全局作用域被删除
- javascript - 在 JavaScript 中构建大字符串;加入方法最有效吗?
- ios - UIStackView 和填充分布 - 不尊重内容拥抱优先级
- python - 如何使 pyqt gui 刷新
- sql - SQL Server:按年份汇总行并将行值命名为当前年份的“当前”和去年的“上一个”
- python - 如何确定 setup.py 中实际需要哪些要求?
- c++11 - 错误:使用 make all 编译 Liquibook 时没有名为 'emplace' 的成员
- javascript - JavaScript 意外的令牌语法错误
- angular - Angular 6 材质表数据源
- sql - SQL SRSS 创建一个在添加用户输入值后更改的列表