无法使用其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 (将#修改为@)