InvalidOperationException: <Error xmlns=''> was not expected.

Julio Bello 201 Reputation points

Hi, Everybody!

I need to deserialize the following XML...

<?xml version="1.0" encoding="utf-8"?>
	<Message> There is an error in....</Message>

I defined the following class...

public class Error { 

	public string Message { get; set; } 

When I attempt to deserialize...

        private TResponse XmlStringToObject<TResponse>(string xmlString)
            using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
                var xmlSerializer = new XmlSerializer(typeof(TResponse));

                return (TResponse)xmlSerializer.Deserialize(memoryStream);

I get the following error message...

System.InvalidOperationException HResult=0x80131509 Message=There is an error in XML document (1, 40). Source=<Cannot evaluate the exception source> Inner Exception 1: InvalidOperationException: <Error xmlns=''> was not expected.

What am I doingwrong?

Microsoft Technologies based on the .NET software framework.
2,049 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,973 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: The period of time during which a program is being executed in a computer.
1,042 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 102.4K Reputation points

    I think that TResponse was not Error in your call of XmlStringToObject<TResponse>.

    Or maybe the real details were different.

    0 comments No comments