在 C# 中反序列化 XML 时出现此错误的原因是什么?
What causes this error deserializing XML in C#?
我在 C# 中反序列化 XML 时出现以下错误,尽管我已在我的 class 中的 XMLType 和 XMLRoot 属性中列出了此命名空间。
<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 class 看起来像:
[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();
我错过了什么?
类型不匹配:您正在尝试反序列化为 ProjectRegistrationInterface 类型的实例,但使用的是为 ProductRegistrationInfo[=14= 类型创建的序列化程序].
我在 C# 中反序列化 XML 时出现以下错误,尽管我已在我的 class 中的 XMLType 和 XMLRoot 属性中列出了此命名空间。
<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 class 看起来像:
[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();
我错过了什么?
类型不匹配:您正在尝试反序列化为 ProjectRegistrationInterface 类型的实例,但使用的是为 ProductRegistrationInfo[=14= 类型创建的序列化程序].