无法在C#中映射XML序列化数据集合

时间:2013-06-11 15:48:19

标签: .net c#-4.0 xml-serialization

<WebxApi> 
<Search>
  <SessionId>49B9BB6E-D2BD-427E-BB38-C3BD37B216E1</SessionId> 
   </Search>
 <Search>
  <SessionId>49B9BB6E-D2BD-427E-BB38-C3BD37B216E1</SessionId> 
   </Search>
 <Search>
  <SessionId>651CE157-1ED7-4644-A6A6-93CE1FEA522D</SessionId> 
   </Search>
</WebxApi>

我在搜索的输入请求中有这个xml我试图使用以下代码反序列化。

 var serializer = new XmlSerializer(typeof(WebxApi));
                            var sReader = new StringReader(sw.ToString());
                           var request = serializer.Deserialize(XmlReader.Create(sReader)) as WebxApi;


[Serializable]
    public class WebxApi
    {

 private List<SearchRequest> search;
 public WebxApi()
        {
            this.Search= new List<SearchRequest>();
        }
             [System.Xml.Serialization.XmlArrayAttribute()]
        public List<SearchRequest> Search
        {
            get
            {
                return search ;
            }
            set
            {
                search = value;
            }
        }
}

 [Serializable]
    public partial class SearchRequest
    {

        public string SessionId { get; set; } 

    }

所有过程都成功完成,但当我尝试获取搜索列表时,给我计数= 0

任何人都可以告诉我哪里出错了

由于

1 个答案:

答案 0 :(得分:0)

  

我可以使用以下代码修复此xml输入搜索请求。

[Serializable]
 public class WebxApi{
 private PaymentSearchRequest[] paymentSearchField;

        [DataMember]
        [System.Xml.Serialization.XmlElementAttribute("Search", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public PaymentSearchRequest[] Search
        {
            get
            {
                return this.paymentSearchField;
            }
            set
            {
                this.paymentSearchField = value;
            }
        }
    }


    [System.SerializableAttribute()]
    public partial class PaymentSearchRequest
    {

        private string sessionIdField;

        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string SessionId
        {
            get
            {
                return this.sessionIdField;
            }
            set
            {
                this.sessionIdField = value;
            }
        }

    }