【问题标题】:XML Deserialization Error in C# - InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> was not expectedC# 中的 XML 反序列化错误 - InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> 不是预期的
【发布时间】:2018-07-09 12:36:08
【问题描述】:

我调用客户的网络服务。它返回一个 XMLElement。 我想反序列化这个结果,但出现错误:

InvalidOperationException: &lt;element xmlns='http://www.w3.org/2001/XMLSchema'&gt; 不是预期的。

我尝试如下:

XElement root = XElement.Parse(result.OuterXml);
var query = root.Descendants().FirstOrDefault(r => r.FirstAttribute != null && r.FirstAttribute.Value == "ZER_ENTEGRASYON");
XmlSerializer XML = new XmlSerializer(typeof(ZerDataList));

using (var reader = new StringReader(query.ToString()))
{
    var serializer = new XmlSerializer(typeof(ZerDataList));
    var someTest = serializer.Deserialize(reader) as ZerDataList;
}

result.OuterXml:

<xs:element name="ZER_ENTEGRASYON" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="ZER_DAYSYIKTRANSIT">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="SIPARIS_ID" type="xs:int" minOccurs="0" />
            <xs:element name="BOLUM_ID" type="xs:int" minOccurs="0" />
            <xs:element name="SORUMLU" type="xs:string" minOccurs="0" />
            <xs:element name="SAP_SIPARIS_NO" type="xs:string" minOccurs="0" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:choice>
  </xs:complexType>
</xs:element>

ZerDataList:

[Serializable()]
[System.Xml.Serialization.XmlRoot("ZER_ENTEGRASYON")]
[System.Xml.Serialization.XmlTypeAttribute("element",Namespace = "http://www.w3.org/2001/XMLSchema")]
public class ZerDataList 
{
    [XmlElement("ZER_DAYSYIKTRANSIT")]
    public ZerData[] ZerData { get; set; }
}
[Serializable()]
public class ZerData
{       
    [XmlElement("SIPARIS_ID")]
    public int SIPARIS_ID { get; set; }
    [XmlElement("BOLUM_ID")]
    public int BOLUM_ID { get; set; }
    [XmlElement("SORUMLU")]
    public string SORUMLU { get; set; }
    [XmlElement("SAP_SIPARIS_NO")]
    public string SAP_SIPARIS_NO { get; set; }
 }

任何帮助将不胜感激。

【问题讨论】:

    标签: c# xml xml-deserialization


    【解决方案1】:

    问题就在这里:

    [System.Xml.Serialization.XmlTypeAttribute("element",Namespace = "http://www.w3.org/2001/XMLSchema")]
    

    命名空间不能映射为元素,因为它不是元素。因此错误:

    InvalidOperationException: http://www.w3.org/2001/XMLSchema'> 不是预期的。

    你应该这样使用它:

       [XmlElement(
       Namespace = "http://www.cpandl.com")]
    

    取自Microsoft docs

    【讨论】:

    • 感谢您的建议。但是 XmlElement 不支持类
    • 是的,因为元素不能处于类级别,只能处于属性级别,这是问题的根源。
    • 我编辑了这个但没有工作: [Serializable()] public class ZerDataRoot { [XmlElement("ZER_ENTEGRASYON", Namespace = "w3.org/2001/XMLSchema")] public ZerDataList ZerDataList { get; set; } } [Serializable()] [System.Xml.Serialization.XmlRoot("ZER_ENTEGRASYON", Namespace = "w3.org/2001/XMLSchema")] public class ZerDataList { [XmlElement("ZER_DAYSYIKTRANSIT")] public ZerData[] ZerData { get; set; } }跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    相关资源
    最近更新 更多