简体   繁体   English

是什么导致此错误在C#中反序列化XML?

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

I am getting the following error while deserializing an XML in C# despite having listed this namespace on the XMLType and XMLRoot attributes in my class. 尽管在类中的XMLType和XMLRoot属性中列出了此命名空间,但在C#中反序列化XML时却遇到以下错误。

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

The XML looks like this: 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>

The ProductRegistrationInterface class looks like: 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; }        
}

And finally here is the code I am using: 最后这是我正在使用的代码:

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

What am I missing? 我想念什么?

类型不匹配:您正在尝试反序列化为类型ProjectRegistration Interface的实例,但是使用为类型ProductRegistration Info创建的序列化程序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM