是否可以在SOAP信封中包含完整的XmlDocument?
我尝试了无数的方法,在包含dataFile
的XML定义时导致500服务器错误,因此包含了RemoveXmlDefinition
来处理它,现在我收到400错误请求错误。
无论我尝试什么,我最终都会使用最终SOAP信封中包含格式的XML,我只需要一个字符串。
public string RemoveXmlDefinition(string xmlDocumentInnerXml)
{
var xDocument = XDocument.Parse(xmlDocumentInnerXml);
xDocument.Declaration = null;
var regex = new Regex(@">\s*<");
return regex.Replace(xDocument.ToString(), "><");
}
var dataFile = System.Web.HttpContext.Current.Server.MapPath("\\XML\\Test.xml");
var debug = "File Name = " + dataFile + "<br />";
var xmlDocument = new XmlDocument();
try
{
xmlDocument.XmlResolver = null;
xmlDocument.PreserveWhitespace = false;
xmlDocument.Load(dataFile);
}
catch (XmlException xmlException)
{
var test = xmlException.Message;
debug = debug + "Error Code = " + xmlException.GetHashCode() + "<br />";
debug = debug + "Error Reason = " + xmlException.InnerException + "<br />";
debug = debug + "Error Line = " + xmlException.LineNumber + "<br />";
}
var soapfile = Server.MapPath("/XML/Soap.xml");
if (System.IO.File.Exists(soapfile))
{
System.IO.File.Delete(soapfile);
}
var xmlTextWriter = new XmlTextWriter(dataFile, Encoding.UTF8) {Formatting = Formatting.None};
xmlDocument.Save(xmlTextWriter);
xmlTextWriter.Close();
string xmlDocumentInnerXml = RemoveXmlDefinition(xmlDocument.InnerXml);
using (var writer = new XmlTextWriter(soapfile, Encoding.UTF8) { Formatting = Formatting.Indented })
{
writer.WriteStartDocument();
writer.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
writer.WriteStartElement("soap", "Body", null);
writer.WriteStartElement("XmlString");
writer.WriteAttributeString("xmlns", "http://tempuri.org/");
writer.WriteFullElementString("xmlDocumentInnerXml", xmlDocumentInnerXml);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
}
xmlDocumentInnerXml
<Affiliate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" leadnumber="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id=""><Details><Amount></Amount><Forename></Forename><Surname></Surname><Tel></Tel><Email></Email></Details></Affiliate>
的SoapEnvelope
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<XmlString xmlns="http://tempuri.org/">
<xmlDocumentInnerXml>
<Affiliate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" leadnumber="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="">
<Details>
<Amount />
<Forename />
<Surname />
<Tel />
<Email />
</Details>
</Affiliate>
</xmlDocumentInnerXml>
</XmlString>
</soap:Body>
</soap:Envelope>
我已经列出了样本SOAP 1.1请求XmlString
期望如下:
XmlString
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<XmlString xmlns="http://tempuri.org/">
<xmlDocumentInnerXml>string</xmlDocumentInnerXml>
</DmAppString>
</soap:Body>
</soap:Envelope>
非常感谢任何帮助。
答案 0 :(得分:1)
我怀疑错误的原因是您使用xmlDocumentInnerXml
方法将XML直接写入WriteFullElementString
方法中的WriteRaw
:
public static void WriteFullElementString(this XmlTextWriter writer, string localName, string value)
{
writer.WriteStartElement(localName);
writer.WriteRaw(value);
writer.WriteFullEndElement();
}
这可能会导致一些架构验证错误。为了将XML字符串包含为CData块并对其进行正确编码,您可以使用WriteCData
- 方法代替WriteRaw
:
public static void WriteFullElementString(this XmlTextWriter writer, string localName, string value)
{
writer.WriteStartElement(localName);
writer.WriteCData(value);
writer.WriteFullEndElement();
}