简体   繁体   中英

Deserialize response in object C#

I have a problem with deserialize xml response

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());

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll Additional information: There is an error in XML document (0, 0).

response:

<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));
            }
        }
    }

Item:

 [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));
            }
        }
    }

It looks like you haven't loaded the document into doc.

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

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

You are not telling the serialiser what the default namespace is.

Try this:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM