反序列化 XML 给我 null 而不是对象
Deserializing XML gives me null instead of the object
这是我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//WorldPay//DTD WorldPay PaymentService v1//EN"
"http://dtd.worldpay.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="ABC">
<reply>
<error code="4">
<![CDATA[Security violation]]>
</error>
</reply>
</paymentService>
我将其反序列化为 类 使用他们提供的 XSD 创建的:
var responseStreamReader = new StreamReader(response.GetResponseStream());
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "paymentService";
XmlSerializer mySerializer = new XmlSerializer(typeof(paymentService), xRoot);
var someResponse = (paymentService) mySerializer.Deserialize(responseStreamReader);
它反序列化了 paymentService、version、merchantCode,但是 Item
属性 是 null
。
这是架构的一部分:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/worldpa
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/worldpay", IsNullable = false
public partial class paymentService
{
private object itemField;
private paymentServiceVersion versionField;
private string merchantCodeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("inquiry", typeof (inquiry))]
[System.Xml.Serialization.XmlElementAttribute("modify", typeof (modify))]
[System.Xml.Serialization.XmlElementAttribute("notify", typeof (notify))]
[System.Xml.Serialization.XmlElementAttribute("reply", typeof (reply))]
[System.Xml.Serialization.XmlElementAttribute("submit", typeof (submit))]
[System.Xml.Serialization.XmlElementAttribute("verify", typeof (verify))]
public object Item
{
get { return this.itemField; }
set { this.itemField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute]
public paymentServiceVersion version
{
get { return this.versionField; }
set { this.versionField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute(DataType = "NMTOKEN")]
public string merchantCode
{
get { return this.merchantCodeField; }
set { this.merchantCodeField = value; }
}
}
我希望项目是一个 reply
对象。
我可能做错了什么?
我已经解决了这个问题,可能不是最好的方式,但为了其他人的利益..
我刚刚从所有内容中删除了命名空间。
我的模式是用 XSD.exe 生成的,我删除了对 Namespace = "http://tempuri.org/worldpay"
的所有引用,这修复了它。这也意味着我不必乱用 XmlRootAttribute。
这是我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//WorldPay//DTD WorldPay PaymentService v1//EN"
"http://dtd.worldpay.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="ABC">
<reply>
<error code="4">
<![CDATA[Security violation]]>
</error>
</reply>
</paymentService>
我将其反序列化为 类 使用他们提供的 XSD 创建的:
var responseStreamReader = new StreamReader(response.GetResponseStream());
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "paymentService";
XmlSerializer mySerializer = new XmlSerializer(typeof(paymentService), xRoot);
var someResponse = (paymentService) mySerializer.Deserialize(responseStreamReader);
它反序列化了 paymentService、version、merchantCode,但是 Item
属性 是 null
。
这是架构的一部分:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/worldpa
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/worldpay", IsNullable = false
public partial class paymentService
{
private object itemField;
private paymentServiceVersion versionField;
private string merchantCodeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("inquiry", typeof (inquiry))]
[System.Xml.Serialization.XmlElementAttribute("modify", typeof (modify))]
[System.Xml.Serialization.XmlElementAttribute("notify", typeof (notify))]
[System.Xml.Serialization.XmlElementAttribute("reply", typeof (reply))]
[System.Xml.Serialization.XmlElementAttribute("submit", typeof (submit))]
[System.Xml.Serialization.XmlElementAttribute("verify", typeof (verify))]
public object Item
{
get { return this.itemField; }
set { this.itemField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute]
public paymentServiceVersion version
{
get { return this.versionField; }
set { this.versionField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlAttribute(DataType = "NMTOKEN")]
public string merchantCode
{
get { return this.merchantCodeField; }
set { this.merchantCodeField = value; }
}
}
我希望项目是一个 reply
对象。
我可能做错了什么?
我已经解决了这个问题,可能不是最好的方式,但为了其他人的利益..
我刚刚从所有内容中删除了命名空间。
我的模式是用 XSD.exe 生成的,我删除了对 Namespace = "http://tempuri.org/worldpay"
的所有引用,这修复了它。这也意味着我不必乱用 XmlRootAttribute。