微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用其XSD从REST服务反序列化HttpResponse消息以生成C#类

如何解决无法使用其XSD从REST服务反序列化HttpResponse消息以生成C#类

我有一个项目,我们在使用另一个公司的REST api。他们提供了一个XSD,用于定义各种对象/元素及其属性等。

一个叫QueryResponse的东西将包含基于我从第三方调用的Query REST函数的各种数据。

但是,在返回的XML中,他们将其包装如下,这就是返回的内容

<Envelope xmlns="http://schemas.some.org/soap/envelope/">
     <Body>
          <QueryResponse xmlns="https://erpm.some.org/rpm/xml">
               <PeakLoadSummarySet>
                    <PeakLoadSummary Day="2018-03-10">
                         ... other data associated with a PeakLoadSummary type
                    </PeakLoadSummary>
                    .... PeakLoadSummary can repeat
               ... varIoUs other elements they define that can repeat.
               </PeakLoadSummarySet>
          </QueryResponse>
     </Body>
</Envelope>

然后,我从HttpResponse对象中提取XML,以将其反序列化为QueryResponse C#POCO,该查询通过XSD命令行实用程序和Visual Studio使用公司提供的XSD文件生成。但是,它们的XSD不会 定义“正文”或“信封”元素。 QueryResponse(还有另一个叫QueryRequest)是它们的顶级元素,所有其他项都定义为包装在QueryRequest或QueryResponse文档中。

我试图在Visual Studio 2019中的C#中按如下方式反序列化它:

// HttpResponse has already returned so I extract the content which would be the XML listed above.
string responseString = await msg.Content.ReadAsstringAsync();
MemoryStream stm = new MemoryStream();
StreamWriter stw = new StreamWriter(stm);

stw.Write(responseString);
stw.Flush();
stm.Position = 0;

XmlSerialize ser = new XmlSerializer(typeof(QueryResponse));
QueryResponse response = ser.Deserialize(stm) as QueryReponse;

调用反序列化时,它将引发以下错误

system.invalidOperationException : there is an error in XML document (1,4).
---- system.invalidOperationException : <Envelope xmlns='http://schemas.some.org/soap/envelope/'> was not expected.

只需重申一下,他们的XSD不会定义Envelope或Body标签。我是这一切的新手,所以不确定如何处理吗?我知道我可以通过使用选择性粘贴粘贴XML到Visual Studio中来生成代码,但这是正确的方法吗?当他们没有完全指定其XML消息格式而是仅指定其一部分时,正确的处理这种情况的方法是什么?

更新:根据请求,以下是从XSD生成的类:

    namespace Schema {
        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd","4.8.3928.0")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,Namespace="https://erpm.some.org/rpm/xml")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace="https://erpm.some.org/rpm/xml",IsNullable=false)]
        public partial class QueryResponse {
             private object itemField;
    
             /// <remarks/>
             [System.Xml.Serialization.XmlElementAttribute("PeakLoadSummarySet",typeof(PeakLoadSummarySetType))]
    
             public object Item {
                 get {
                     return this.itemField;
                 }
                 set {
                     this.itemField = value;
                 }
             }
         }
    
        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd","4.8.3928.0")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://erpm.some.org/rpm/xml")]
        public partial class PeakLoadSummarySetType {
            
            private PeakLoadSummaryType[] peakLoadSummaryField;
            
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("PeakLoadSummary")]
            public PeakLoadSummaryType[] PeakLoadSummary {
                get {
                    return this.peakLoadSummaryField;
                }
                set {
                    this.peakLoadSummaryField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd","4.8.3928.0")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]     
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://erpm.some.org/rpm/xml")]
    
        public partial class PeakLoadSummaryType {
            
            private string lSEField;
            
            private string zoneNameField;
            
            private string areaNameField;
    
            /// <remarks/>
            public string LSE {
                get {
                    return this.lSEField;
                }
                set {
                    this.lSEField = value;
                }
            }
            
            /// <remarks/>
            public string ZoneName {
                get {
                    return this.zoneNameField;
                }
                set {
                    this.zoneNameField = value;
                }
            }
            
            /// <remarks/>
            public string AreaName {
                get {
                    return this.areaNameField;
                }
                set {
                    this.areaNameField = value;
                }
            }
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。