有可能在类XMLTypeAttribute中有两个名称空间来处理2个soap响应的反序列化?
问题描述:
我有了以下声明一个类:有可能在类XMLTypeAttribute中有两个名称空间来处理2个soap响应的反序列化?
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{
private EnvelopeBody bodyField;
/// <remarks/>
public EnvelopeBody Body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
}
.
. another code generated here based on response XML...
.
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "order")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "order", IsNullable = false)]
public partial class insertResponse
{
private insertResponseOut outField;
/// <remarks/>
public insertResponseOut @out
{
get
{
return this.outField;
}
set
{
this.outField = value;
}
}
}
反序列化时含<insertResponse xmlns="order">
XML
响应,我可以成功地做到这一点。
我还有一个XML SOAP响应,这是完全一样的格式,我想使用相同的类,但与不同的XmlTypeAttribute
:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "customer")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "customer", IsNullable = false)]
public partial class insertResponse
{
private insertResponseOut outField;
/// <remarks/>
public insertResponseOut @out
{
get
{
return this.outField;
}
set
{
this.outField = value;
}
}
}
目前,我有一个方法处理SOAP响应反序列化:
private Envelope DeserializeSoapResponse<T>(string soapResponse)
{
var serealizer = new XmlSerializer(typeof(T));
Envelope result;
using (TextReader reader = new StringReader(soapResponse))
{
result = (Envelope)serealizer.Deserialize(reader);
}
return result;
}
soapResponse参数不是XML的路径,它是表示来自服务器的xml SOAP响应的字符串。
我也尝试过使用自定义XML阅读器:
public class CustomXmlReader: System.Xml.XmlTextReader
{
public CustomXmlReader(string url) : base(url) { }
public override string NamespaceURI
{
get
{
if (base.NamespaceURI == "order")
return "customer";
return base.NamespaceURI;
}
}
}
的建议,但我有以下错误:illegal character in path
,因为如我所料,我需要的URL发送到SOAP响应,但发送字符串
我该如何做这样的事情?是否可以为XmlTypeAttribute
或XmlRootAttribute
或XmlRootAttribute
定义多个名称空间
答
为此,您可以使用自定义xml读取器。
public class CustomXmlReader : XmlTextReader
{
// Define other required constructors
public CustomXmlReader(string url) : base(url) { }
public CustomXmlReader(TextReader reader) : base(reader) { }
public override string NamespaceURI
{
get
{
if (base.NamespaceURI == "order")
return "customer";
return base.NamespaceURI;
}
}
}
它把一个order
命名为customer
命名空间的飞行。
因此,你的类应该只有一个customer
namespase声明:
[XmlType(AnonymousType = true, Namespace = "customer")]
[XmlRoot(Namespace = "customer", IsNullable = false)]
public partial class insertResponse
照常使用:
string xml = "your xml here";
insertResponse response;
using (var stringReader = new StringReader(xml))
using (var xmlReader = new CustomXmlReader(stringReader))
response = (insertResponse)xs.Deserialize(xmlReader);
我试图用这种方法,但有一个例外,说'非法字符路径'。我认为这是由发送表示XML SOAP响应的字符串而不是路径本身引起的。但该逻辑适用于“订单”反序列化 – gene
@gene - 我写道:定义其他必要的[构造函数](https://msdn.microsoft.com/en-us/library/system.xml.xmltextreader(v = vs。 110)的.aspx#ANCHOR_2)!查看更新。 –
我按照你的建议做了,但仍然有同样的错误 – gene