我想反序列化XML到C#对象。反序列化到对象
我的XML是:
<StaffingOrder xmlns='StaffinfOrderNamespace' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<OrderId idOwner="VMSAccess">
<IdValue name='RequisitionID'>1</IdValue>
<IdValue name='KellyBranch'>3319</IdValue>
</OrderId>
</StaffingOrder>
我的班级是:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[XmlRoot("StaffingOrder")]
public class OrderId
{
[XmlElement(ElementName = "RequisitionID")]
public int RequisitionID { get; set; }
[XmlElement(ElementName = "KellyBranch")]
public int KellyBranch { get; set; }
}
我的WCF服务合同是:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "UploadOrderDocument",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
[XmlSerializerFormat]
OrderId UploadOrderDocument(string hrXML);
我试图为反序列化:
public OrderId UploadOrderDocument(string hrXMLString)
{
OrderId orderId = new OrderId();
using (StringReader sr = new StringReader(hrXMLString))
{
XmlSerializer xs = new XmlSerializer(typeof(OrderId));
orderId = (OrderId)xs.Deserialize(sr);
var temp = orderId.RequisitionID;
}
return orderId;
}
反序列化时我没有收到任何错误,但没有得到任何值RequisitionID
。
[反序列化具有多个元素属性的XML文件 - 属性不是反序列化]的可能重复(http://stackoverflow.com/questions/14245846/deserializing-xml-file-with-multiple-element-attributes-attributes-are -not-des) – niksofteng