【问题标题】:HttpResponse namespace/prefix does not match with the WSDL format on Serializing the XMLHttpResponse 命名空间/前缀与序列化 XML 上的 WSDL 格式不匹配
【发布时间】:2020-09-23 06:19:27
【问题描述】:

当前反应

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header />
    <s:Body>
        <CreateCaseResponse xmlns="http://">
            <CreateCaseResult>
                <ErrorMessage>\n</ErrorMessage>
                <Successful>true</Successful>
                <CaseNumber></CaseNumber>
                <Recno></Recno>
            </CreateCaseResult>
        </CreateCaseResponse>
    </s:Body>
</s:Envelope>

在上面的响应中,CreateCaseResult 没有namespace,并且与下面的expected response 相比,数据成员(CaseNumber and Recno) 缺少prefix

预期响应

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <CreateCaseResponse xmlns="">
         <CreateCaseResult xmlns:a="" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <ErrorDetails i:nil="true"/>
            <ErrorMessage></ErrorMessage>
            <Successful>true</Successful>
            <a:CaseNumber></a:CaseNumber>
            <a:Recno></a:Recno>
         </CreateCaseResult>
      </CreateCaseResponse>
   </s:Body>
</s:Envelope>

序列化代码如下-

 private static string MakeSerializedWithSoapEnvelope(object obj, string defaultNamespace)
        {
            var respFormat = @"
<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
  <s:Header />
  <s:Body>
    {0}
  </s:Body>
</s:Envelope>";

            var serializer = new XmlSerializer(obj.GetType(), defaultNamespace);
            var settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", defaultNamespace);

            using (var stringWriter = new StringWriter())
            {
                using (var xmlWriter = XmlWriter.Create(stringWriter, settings))
                {
                    serializer.Serialize(xmlWriter, obj, ns);
                    return string.Format(respFormat, stringWriter.ToString());
                }
            }
        }

这里defaultNamespace 被传递为``。

请帮助我了解应该对代码进行哪些更改以获得desired and expected output/response.

提前致谢。

【问题讨论】:

  • 序列化器自己并不知道对象中有多个命名空间需要序列化。您指定默认命名空间,这就是序列化程序添加到所有内容的内容。如果您需要在特定元素上使用更多命名空间,最好在类中定义要序列化的对象并将 xml 序列化信息添加到特定元素。
  • @martijn 我可以举个例子吗?
  • 查看@jdweng 提供的答案。这将为您提供解决此问题所需的所有指导。

标签: c# xml serialization wsdl httpresponse


【解决方案1】:

尝试以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;


namespace ConsoleApplication169
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("s", "http://schemas.xmlsoap.org/soap/envelope/");
            ns.Add("", "http://software-innovation.com/SI.Data");
            ns.Add("a", "http://schemas.datacontract.org/2004/07/SI.Data.Contracts.WS");
            ns.Add("i", "http://www.w3.org/2001/XMLSchema-instance");

            Envelope envelope = new Envelope();
            Body body = new Body();
            envelope.Body = body;
            CreateCaseResult results = new CreateCaseResult();
            body.CreateCaseResult = new List<CreateCaseResult>() { results};
            results.ErrorDetails = new Detail() { nil = true };

            results.Successful = true;
            results.CaseNumber = "20/00239";
            results.Recno = "200245";
            results.ErrorMessage = "";

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME, settings);

            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
            serializer.Serialize(writer, envelope, ns);
 

        }
    }
    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
    }
    public class Body
    {
        [XmlArray(ElementName = "CreateCaseResponse", Namespace = "")]
        [XmlArrayItem(ElementName = "CreateCaseResult", Namespace = "")]
        public List<CreateCaseResult> CreateCaseResult { get; set; }
    }
    public class CreateCaseResult 
    {
        public Detail ErrorDetails { get; set; }
        public string ErrorMessage { get; set; }
        public Boolean Successful { get; set; }
        [XmlElement(ElementName = "CaseNumber", Namespace = "http://schemas.datacontract.org/2004/07/SI.Data.Contracts.WS")]
        public string CaseNumber { get; set; }
        [XmlElement(ElementName = "Recno", Namespace = "http://schemas.datacontract.org/2004/07/SI.Data.Contracts.WS")]
        public string Recno { get; set; }
    }
    public class Detail
    {
        [XmlAttribute(AttributeName = "nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public Boolean nil { get; set; }
    }
}

【讨论】:

    猜你喜欢
    • 2011-01-21
    • 2013-05-02
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多