【问题标题】:Deserialize response in object C#反序列化对象 C# 中的响应
【发布时间】:2017-04-18 14:31:02
【问题描述】:

我在反序列化 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>

运行查询回复:

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

运行查询结果集:

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

【问题讨论】:

  • 如果您有来自 Web 服务调用的类型化响应,您真的不清楚为什么需要摆弄 Xml 序列化。你不能只使用doc 响应对象吗?
  • if(response!=null) (RunQueryReply)s.Deserialize(response.CreateReader());

标签: c# xml web-services


【解决方案1】:

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

试试这个:

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

【讨论】:

    【解决方案2】:

    您似乎还没有将文档加载到文档中。

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

    【讨论】:

    • 静态 Archive.RunQueryReply DeserializeFromXmlDocument(XDocument doc) { //.Replace("&", "&")); XmlSerializer s = new XmlSerializer(typeof(Archive.RunQueryReply)); var response = doc.Descendants().Where(x => x.Name.LocalName == typeof(Archive.RunQueryReply).Name).FirstOrDefault(); return (Archive.RunQueryReply)s.Deserialize(response.CreateReader()); }
    • 抱歉,@BERR,您粘贴的代码不包含 doc 的创建位置和 XML 的加载位置。
    • doc,它是来自网络服务的响应。字符串soapResult;使用 (WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) { 使用 (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) { soapResult = rd.ReadToEnd(); } 返回肥皂结果; } var dd=CallWebService("cmfs:9115/CMBSpecificWebService/services/…); var doc = XDocument.Parse(dd);
    • XDocument.Parse(dd) 行中 dd 的值是否正确(即 xml); ?
    猜你喜欢
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多