簡體   English   中英

是什么導致此錯誤在C#中反序列化XML?

[英]What causes this error deserializing XML in C#?

盡管在類中的XMLType和XMLRoot屬性中列出了此命名空間,但在C#中反序列化XML時卻遇到以下錯誤。

<ProductRegistrationInterface xmlns='http://www.4cs.com/CLMSchema'> was not expected.

XML如下所示:

  <?xml version="1.0"?>
  <ns4CS:ProductRegistrationInterface 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:ns4CS="http://www.4cs.com/CLMSchema">
    <ns4CS:MessageHeader>
      <TargetReference>Some Value</TargetReference>
    </ns4CS:MessageHeader>
    <ProductRegistrations>
      <ProductRegistration>...</ProductRegistration>
      <ProductRegistration>...</ProductRegistration>
      <ProductRegistration>...</ProductRegistration>
    </ProductRegistrations>
  </ns4CS:ProductRegistrationInterface>

ProductRegistrationInterface類如下所示:

[XmlType(AnonymousType = true, Namespace = "http://www.4cs.com/CLMSchema")]
[XmlRoot(Namespace = "http://www.4cs.com/CLMSchema", IsNullable = false)]
public class ProductRegistrationInterface
{
    /// <summary>
    /// Collection of <see cref="ProductRegistration"/> class.
    /// </summary>
    [XmlArray(Namespace = "")]
    [XmlArrayItem(nameof(ProductRegistration), IsNullable = false)]
    public ProductRegistration[] ProductRegistrations { get; set; }        
}

最后這是我正在使用的代碼:

XmlSerializer xs = new XmlSerializer(typeof(ProductRegistrationInfo));
StreamReader sr = new StreamReader(@"Test.xml");
ProductRegistrationInfo s = (ProductRegistrationInfo)xs.Deserialize(sr);
sr.Close();

我想念什么?

類型不匹配:您正在嘗試反序列化為類型ProjectRegistration Interface的實例,但是使用為類型ProductRegistration Info創建的序列化程序。

暫無
暫無

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

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