反序列化对象C#中的响应

时间:2017-04-18 14:31:03

标签: c# xml web-services

我遇到了反序列化xml响应的问题

string soapResult; 
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) 
{
   using (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) 
   { 
       soapResult = rd.ReadToEnd(); 
   } 
   return soapResult; 
} 
var dd=CallWebService("cmfs:9115/CMBSpecificWebService/services/‌​…"); 
var doc = XDocument.Parse(dd); 
XmlSerializer s = new XmlSerializer(typeof(RunQueryReply));
var response = doc.Descendants().Where(x => x.Name.LocalName == typeof(RunQueryReply).Name).FirstOrDefault();
       (RunQueryReply)s.Deserialize(response.CreateReader());
  

System.Xml.dll中发生了未处理的“System.InvalidOperationException”类型异常   附加信息:XML文档(0,0)中存在错误。

响应:

<ns1:RunQueryReply xmlns="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:ns1="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema">
  <ns1:RequestStatus success="true"></ns1:RequestStatus>
  <ns1:ResultSet count="5">
    <ns1:Item URI="http://test:9115/TestWebService/CMBGetPIDUrl?pid=93 3 ICM6 i2mnlsdd13 RB_ClientN59 26 A1001001A16K02B12842F0117018 A16K02B12842F011702 14 2680&amp;server=icmnlsdb&amp;dsType=ICM">
      <ns1:ItemXML />
    </ns1:Item>
    <ns1:Item URI="http://test:9115/TestWebService/CMBGetPIDUrl?pid=93 3 ICM6 i2mnlsdd13 RB_ClientN59 26 A1001001A16K02B41832H0124418 A16K02B41832H012442 12 2680&amp;server=icmnlsdb&amp;dsType=ICM">
      <ns1:ItemXML />
    </ns1:Item>
    <ns1:Item URI="http://test:9115/TestWebService/CMBGetPIDUrl?pid=93 3 ICM6 i2mnlsdd13 RB_ClientN59 26 A1001001A16K02B42003D0124618 A16K02B42003D012431 12 2680&amp;server=icmnlsdb&amp;dsType=ICM">
      <ns1:ItemXML />
    </ns1:Item>
    <ns1:Item URI="http://test:9115/TestWebService/CMBGetPIDUrl?pid=93 3 ICM6 i2mnlsdd13 RB_ClientN59 26 A1001001A16K11B11808G3379311 A16K11B11808G337431 12 2680&amp;server=icmnlsdb&amp;dsType=ICM">
      <ns1:ItemXML />
    </ns1:Item>
    <ns1:Item URI="http://test:9115/TestWebService/CMBGetPIDUrl?pid=93 3 ICM6 i2mnlsdb13 RB_ClientN59 26 A1001001A17A25B11425B0804018 A17A25B11425B082401 11 2680&amp;server=icmnlsdb&amp;dsType=ICM">
      <ns1:ItemXML />
    </ns1:Item>
  </ns1:ResultSet>
</ns1:RunQueryReply>

RunQueryReply:

 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema")]
    public partial class RunQueryReply : object, System.ComponentModel.INotifyPropertyChanged {

        private RequestStatus requestStatusField;

        private RunQueryReplyResultSet resultSetField;

        private MTOMAttachment[] mtomRefField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=0)]
        public RequestStatus RequestStatus {
            get {
                return this.requestStatusField;
            }
            set {
                this.requestStatusField = value;
                this.RaisePropertyChanged("RequestStatus");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=1)]
        public RunQueryReplyResultSet ResultSet {
            get {
                return this.resultSetField;
            }
            set {
                this.resultSetField = value;
                this.RaisePropertyChanged("ResultSet");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("mtomRef", Order=2)]
        public MTOMAttachment[] mtomRef {
            get {
                return this.mtomRefField;
            }
            set {
                this.mtomRefField = value;
                this.RaisePropertyChanged("mtomRef");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

RunQueryResultSet:

 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema")]
    public partial class RunQueryReplyResultSet : object, System.ComponentModel.INotifyPropertyChanged {

        private Item[] itemField;

        private int countField;

        public RunQueryReplyResultSet() {
            this.countField = 0;
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Item", Order=0)]
        public Item[] Item {
            get {
                return this.itemField;
            }
            set {
                this.itemField = value;
                this.RaisePropertyChanged("Item");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        [System.ComponentModel.DefaultValueAttribute(0)]
        public int count {
            get {
                return this.countField;
            }
            set {
                this.countField = value;
                this.RaisePropertyChanged("count");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

档案:

 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema")]
    public partial class Item : object, System.ComponentModel.INotifyPropertyChanged {

        private System.Xml.XmlElement itemXMLField;

        private string uRIField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order=0)]
        public System.Xml.XmlElement ItemXML {
            get {
                return this.itemXMLField;
            }
            set {
                this.itemXMLField = value;
                this.RaisePropertyChanged("ItemXML");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string URI {
            get {
                return this.uRIField;
            }
            set {
                this.uRIField = value;
                this.RaisePropertyChanged("URI");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

看起来您还没有将文档加载到doc。

var doc = ?????
doc.Load(???xmlfile???);

var response = doc.Descendants().Where(x => x.Name.LocalName == typeof(RunQueryReply).Name).FirstOrDefault();
   (RunQueryReply)s.Deserialize(response.CreateReader());

答案 1 :(得分:0)

您没有告诉序列化器默认命名空间是什么。

试试这个:

var stream = new StringReader(dd);
XmlSerializer s = new XmlSerializer(typeof(RunQueryReply), "http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema");
var a = (RunQueryReply) s.Deserialize(stream);