简体   繁体   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

I call a customer's web service. 我呼叫客户的网络服务。 And it is return a XMLElement. 它返回一个XMLElement。 I want to deserialize this result but I got an error: 我想反序列化此结果,但出现错误:

InvalidOperationException: <element xmlns='http://www.w3.org/2001/XMLSchema'> was not expected. InvalidOperationException:不应预期<element xmlns='http://www.w3.org/2001/XMLSchema'>

I try like below: 我尝试如下:

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

Any help will be much appreciated. 任何帮助都感激不尽。

The issue is here: 问题在这里:

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

a namespace cannot be mapped as an element, because it is not an element. 名称空间不能映射为元素,因为它不是元素。 thus the error: 因此错误:

InvalidOperationException: http://www.w3.org/2001/XMLSchema'> was not expected. InvalidOperationException:不应预期http://www.w3.org/2001/XMLSchema'>。

you should use it like this: 您应该像这样使用它:

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

taken from Microsoft docs . 取自Microsoft文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除 p2:type="&lt;<type> &gt;" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" 来自 xml</type> - Remove p2:type="<<type>>" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" from xml 使用 DataContractSerializer 时删除 xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; - remove xmlns:i=“http://www.w3.org/2001/XMLSchema-instance” when using DataContractSerializer 从名称空间&#39;http://www.w3.org/2001/XMLSchema-instance&#39;期望元素&#39;CustomerLeads&#39; - Expecting element 'CustomerLeads' from namespace 'http://www.w3.org/2001/XMLSchema-instance' 元素http://www.w3.org/2001/XMLSchema:complexType在此上下文中无效 - Element http://www.w3.org/2001/XMLSchema:complexType is invalid in this context C# XPathSelectElement 和 xml 属性 xmlns=“http://www.w3.org/2000/09/xmldsig#” 帮助 - C# XPathSelectElement and xml with attribute xmlns=“http://www.w3.org/2000/09/xmldsig#” Help 具有“ http://www.w3.org/2001/XMLSchema”名称空间的性能影响 - Performance impact of having “http://www.w3.org/2001/XMLSchema” namespace 避免在.Net DataContractSerializer中使用“http://www.w3.org/2001/XMLSchema-instance”命名空间 - Avoiding using the “http://www.w3.org/2001/XMLSchema-instance” namespace with .Net DataContractSerializer SignedXml CanonicalizationMethod - http://www.w3.org/2006/12/xml-c14n11 - SignedXml CanonicalizationMethod - http://www.w3.org/2006/12/xml-c14n11 在 C# 反序列化期间,出现 System.InvalidOperationException: &lt; xmlns=''&gt; 不是预期的 - During C# deserialization, getting System.InvalidOperationException: < xmlns=''> was not expected JWT Header 算法:“hs256”与“http://www.w3.org/2001/04/xmldsig-more#hmac-sha256”相同 - JWT Header algorithm: is "hs256" the same as "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM