c# - 为 SOAP 请求生成 XML 时出错
问题描述
我想在 C3 中创建以下 XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com" xmlns:ns2="example.com">
<SOAP-ENV:Body>
<ns2:NewCustAddDC>
<ns1:CustomerBillToAddress1>Test 123</ns1:CustomerBillToAddress1>
</ns2:NewCustAddDC>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这是我的代码,由于colon
in崩溃SOAP-ENV:Envelope
。在这里可以做什么?
//create the root doc
XDocument rootDoc = new XDocument(new XDeclaration("1.0", "utf-16", "yes"));
//rootDoc.Add(new XElement("xml", "testomg"));
//write the name space
XNamespace ns1 = "http://example.com";
XNamespace ns2 = "http://example.com";
XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
//form the soap envelope and body
var soapEnvelope = new XElement("SOAP-ENV:Envelope",
new XAttribute(XNamespace.Xmlns + "ns1", ns1.NamespaceName),
new XAttribute(XNamespace.Xmlns + "ns2", ns2.NamespaceName),
new XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName)
);
rootDoc.AddFirst(soapEnvelope);
var soapBody = new XElement("SOAP-ENV:Body");
soapEnvelope.Add(soapBody);
//this element holds the NewCustomerAdd element. This is the element the service looks for.
var actionElement = new XElement("NewCustAddDC");
soapBody.Add(actionElement);
var customerBilling = new XElement("CustomerBillToAddress1", "Test 123");
actionElement.Add(customerBilling);
//print the xml
var wr = new StringWriter();
rootDoc.Save(wr);
Console.Write(wr.ToString());
这是错误:
Message = "The ':' character, hexadecimal value 0x3A, cannot be included in a name."
at System.Xml.XmlConvert.VerifyNCName(String name, ExceptionType exceptionType)
at System.Xml.Linq.XName..ctor(XNamespace ns, String localName)
at System.Xml.Linq.XNamespace.GetName(String localName)
at System.Xml.Linq.XName.Get(String...
解决方案
推荐阅读
- ios - 当 AVPlayerViewController 退出FullScreenAnimated 时如何得到通知
- docker - 无法在运行时使用 docker 映像中的环境变量更改文件的内容
- wpf - 适合按钮宽度
- r - R Studio 中的因子分析:公式和 Excel 数据
- django-rest-framework - 在 Django Rest Framework 中实现所需的 as_view 参数
- python - 为 tensorflow 获取 .whl 以支持所有 CPU 指令集
- aurelia - 无法将 aurelia-i18n 中的语言环境文件添加到 dist 文件夹以进行生产
- python - 在 Python 中,从输入读取文件内容并写入输出,但相同的内容不应附加到输出
- php - 我怎样才能获得当前类别的第一篇文章并回显链接?
- c# - bash命令启动后如何保持应用程序打开C#