首页 > 解决方案 > 为 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>

这是我的代码,由于colonin崩溃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...

标签: c#xmlsoap

解决方案


推荐阅读