0

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.

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

The XML looks like this:

 <?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:

[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?

2
  • The error uses a different quote (') than the posted code ("). Can you check both? Commented Feb 15, 2019 at 18:25
  • I think it was an issue with how the error message was showing in the post. I have corrected that now. Commented Feb 15, 2019 at 18:33

1 Answer 1

2

Type mismatch: you are attempting to deserialize into an instance of type ProjectRegistrationInterface, but using a serializer created for the type ProductRegistrationInfo.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it. I don't know how I didn't see that earlier. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.