我具有以下XML类结构:
[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; }
}
[XmlRoot(ElementName = "Body")]
public class Body
{
[XmlElement(ElementName = "CustomerBundleMaintainConfirmation_sync_V1", Namespace = "http://sap.com/xi/SAPGlobal20/Global")]
public CustomerMaintainConfirmation CustomerMaintainConfirmation { get; set; }
}
[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")]
public class CustomerMaintainConfirmation
{
[XmlElement(ElementName = "Log")]
public Log Log { get; set; }
}
[XmlRoot(ElementName = "Log")]
public class Log
{
[XmlElement(ElementName = "MaximumLogItemSeverityCode")]
public string MaximumLogItemSeverityCode { get; set; }
[XmlElement(ElementName = "Item")]
public Item Item { get; set; }
}
[XmlRoot(ElementName = "Item")]
public class Item
{
[XmlElement(ElementName = "TypeID")]
public string TypeID { get; set; }
[XmlElement(ElementName = "CategoryCode")]
public string CategoryCode { get; set; }
[XmlElement(ElementName = "SeverityCode")]
public string SeverityCode { get; set; }
[XmlElement(ElementName = "Note")]
public string Note { get; set; }
}
这是我正在使用的XML:
<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header />
<soap-env:Body>
<n0:CustomerBundleMaintainConfirmation_sync_V1 xmlns:n0="http://sap.com/xi/SAPGlobal20/Global" xmlns:prx="urn:sap.com:proxy:LBK:/1SAI/TAE6F3228CC6D723FF1823E:804">
<Log>
<MaximumLogItemSeverityCode>3</MaximumLogItemSeverityCode>
<Item>
<TypeID>018</TypeID>
<CategoryCode>YES</CategoryCode>
<SeverityCode>0</SeverityCode>
<Note>TestNotes</Note>
</Item>
</Log>
</n0:CustomerBundleMaintainConfirmation_sync_V1>
</soap-env:Body>
</soap-env:Envelope>
当我尝试将此数据反序列化到我的类中时,无论出于何种原因,“ Log”类为null。信封,正文和CustomerMaintainConfirmation全部正确填充。我不知道为什么会这样,但是直到现在我一直在盯着这个,只要我绝对可以确定我的代码中以及其中的某个地方都错了一个错误。
最后一段代码在执行实际反序列化的方法内部:
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return (Envelope)serializer.Deserialize(responseReader);
}
我仍在努力中,但是如果有人可以指出我提供的内容可能遇到的任何问题,请告诉我。
答案 0 :(得分:2)
这是名称空间不一致。通过此更改反序列化:
[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")]
public class CustomerMaintainConfirmation
{
[XmlElement(ElementName = "Log", Namespace = "")]
public Log Log { get; set; }
}
答案 1 :(得分:1)
您只是错过了元素上的空名称空间指示:
[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")]
public class CustomerMaintainConfirmation
{
[XmlElement(ElementName = "Log", Namespace = "")]
public Log Log { get; set; }
}
这是必需的,显然,即使上面的Log
具有一个非空的元素,您的CustomerBundleMaintainConfirmation_sync_V1
元素也具有空的命名空间。
这是代码中唯一更改为make it work的地方,如小提琴所示。