簡體   English   中英

C#中的XML反序列化錯誤-InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> 沒想到

[英]XML Deserialization Error in C# - InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> was not expected

我呼叫客戶的網絡服務。 它返回一個XMLElement。 我想反序列化此結果,但出現錯誤:

InvalidOperationException:不應預期<element xmlns='http://www.w3.org/2001/XMLSchema'>

我嘗試如下:

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

任何幫助都感激不盡。

問題在這里:

[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文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM